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 01:48:53 EDT 2006


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



More information about the Python-list mailing list