471,594 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

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

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
5 1437
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
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
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
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
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.

Similar topics

reply views Thread by Mike P | last post: by
5 posts views Thread by beginner | last post: by
41 posts views Thread by dspfun | last post: by
1 post views Thread by Cowboy \(Gregory A. Beamer\) | last post: by
20 posts views Thread by raylopez99 | last post: by
reply views Thread by Pavel Minaev | last post: by
6 posts views Thread by Stefan Hoffmann | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.