niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » python
Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python"
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
Author Message
Casey Hawthorne
*nix forums beginner


Joined: 12 May 2005
Posts: 26

PostPosted: Fri Jul 21, 2006 3:41 am    Post subject: Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python" Reply with quote

Since there was talk of if-then-else not being allowed in lambda
expressions, the following is from "Dive into Python"

The and-or conditional expression trick from page 41 of "Dive into
Python"

Wrap the arguments in lists and then take the first element.

Quote:
a = ""
b = "second"
(1 and [a] or [b])[0]
''


--
Regards,
Casey
Back to top
Peter Otten
*nix forums Guru


Joined: 20 Feb 2005
Posts: 464

PostPosted: Fri Jul 21, 2006 5:48 am    Post subject: Re: Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python" Reply with quote

Casey Hawthorne wrote:

Quote:
Since there was talk of if-then-else not being allowed in lambda
expressions, the following is from "Dive into Python"

The and-or conditional expression trick from page 41 of "Dive into
Python"

Wrap the arguments in lists and then take the first element.

a = ""
b = "second"
(1 and [a] or [b])[0]
''

# python 2.5
Quote:
a, b = "", 0
a if False else b
0
a if True else b
''


Time to tear out that page. Really.

Peter
Back to top
antroy@gmail.com
*nix forums addict


Joined: 24 May 2006
Posts: 52

PostPosted: Fri Jul 21, 2006 10:04 am    Post subject: Re: Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python" Reply with quote

Quote:
# python 2.5
a, b = "", 0
a if False else b
0
a if True else b
''

Time to tear out that page. Really.

Not quite - 2.5 hasn't been released in its final version yet, and many
projects I should imagine will take a while to upgrade.
Back to top
Peter Otten
*nix forums Guru


Joined: 20 Feb 2005
Posts: 464

PostPosted: Fri Jul 21, 2006 12:08 pm    Post subject: Re: Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python" Reply with quote

Ant wrote:

Quote:

# python 2.5
a, b = "", 0
a if False else b
0
a if True else b
''

Time to tear out that page. Really.

Not quite - 2.5 hasn't been released in its final version yet, and many
projects I should imagine will take a while to upgrade.

Ok, use

if cond:
value = ... # expression that must not be evaluated unless it absolutely
# has to be
else:
value = ... # same thing

which will withstand the test of time. Now tear out that page...

Peter
Back to top
bruno modulix
*nix forums Guru


Joined: 21 Feb 2005
Posts: 819

PostPosted: Fri Jul 21, 2006 12:13 pm    Post subject: Re: Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python" Reply with quote

Peter Otten wrote:
Quote:
Ant wrote:


# python 2.5

a, b = "", 0
a if False else b

0

a if True else b

''

Time to tear out that page. Really.

Not quite - 2.5 hasn't been released in its final version yet, and many
projects I should imagine will take a while to upgrade.


Ok, use

if cond:
value = ... # expression that must not be evaluated unless it absolutely
# has to be
else:
value = ... # same thing

which will withstand the test of time.

But isn't allowed in a lambda !-)



--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb@xiludom.gro'.split('@')])"
Back to top
Peter Otten
*nix forums Guru


Joined: 20 Feb 2005
Posts: 464

PostPosted: Fri Jul 21, 2006 12:36 pm    Post subject: Re: Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python" Reply with quote

Bruno Desthuilliers wrote:

Quote:
But isn't allowed in a lambda !-)

Then tear out that lambada page, too, I was tempted to say, but I will
desist. For now...

Peter
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
The time now is Sat Nov 22, 2008 8:39 pm | All times are GMT
navigation Forum index » Programming » python
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Python proficiency test Kent Johnson python 0 Fri Jul 21, 2006 10:47 am
No new posts Regular Expressions crash course elyob PHP 1 Thu Jul 20, 2006 8:07 pm
No new posts Using python code from Java? fortepianissimo python 4 Thu Jul 20, 2006 5:36 pm
No new posts FAQ 1.9 How does Perl compare with other languages like J... PerlFAQ Server Perl 0 Thu Jul 20, 2006 7:03 am
No new posts Depricated String Functions in Python Anoop python 14 Thu Jul 20, 2006 6:26 am

Loans | Mobile Phone | Samsung | Best Credit Cards | Loans
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2144s ][ Queries: 16 (0.1270s) ][ GZIP on - Debug on ]