trinary operator - if then else

Diez B. Roggisch deets at nospam.web.de
Wed Apr 25 07:31:39 EDT 2007


Michael >>
>> Does Python 2.4 support it?
> 
> Not precisely, but you can *usually* get away with:
> 
> a and b or c

This is really bad advice, as long as you don't explain why it "usually"
works (and often enough not). This for example won't work:

>>> False or '' and 0
''

The reason is that the if-value has to be bool(v) == True, otherwise the
short-circuiting of the and will prevent the 0 from being returned.

So in general, _don't_ use this!

Diez



More information about the Python-list mailing list