Conditional operator in Python?

Tim Peters tim.one at home.com
Sun Apr 1 13:57:20 EDT 2001


>>     (x and [a] or [b])[0]

[Marcin 'Qrczak' Kowalczyk]
> It computes both a and b.

Nope, it doesn't.  If x is true it evaluates only a; if x false, only b.

> A correct solution is
>     (0 and (lambda: a) or (lambda: b))()

Only if x is always false <wink>, repair the typo, and you're still left with
"the usual" namespace visibility surprises.

> Yes, it is ugly.

They both are.  Use if/then/else, for Parrot's sake.





More information about the Python-list mailing list