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

Peter Otten __peter__ at web.de
Fri Jul 21 08:08:05 EDT 2006


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



More information about the Python-list mailing list