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

C

Casey Hawthorne

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 )[0]

''
 
P

Peter Otten

Casey said:
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 )[0]

''


# python 2.5''

Time to tear out that page. Really.

Peter
 
A

Ant

# python 2.5
''

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.
 
P

Peter Otten

Ant said:
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
 
B

Bruno Desthuilliers

Peter said:
Ant wrote:




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 !-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top