By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,536 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,536 IT Pros & Developers. It's quick & easy.

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

P: n/a
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]
''

--
Regards,
Casey
Jul 21 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Casey Hawthorne wrote:
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
>>a, b = "", 0
a if False else b
0
>>a if True else b
''

Time to tear out that page. Really.

Peter
Jul 21 '06 #2

P: n/a
Ant
# 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.

Jul 21 '06 #3

P: n/a
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. Now tear out that page...

Peter
Jul 21 '06 #4

P: n/a
Peter Otten wrote:
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 'o****@xiludom.gro'.split('@')])"
Jul 21 '06 #5

P: n/a
Bruno Desthuilliers wrote:
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

Jul 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.