Equivalent to (a ? b : c) ?

Charles G Waldman cgw at fnal.gov
Mon Dec 20 12:13:05 EST 1999


 > Scott Malraux writes:
 > 
 > > I remember seeing the Python equivalent to C's (a?b:c) inline if
 > > statement, but I can't find it for the life of me... can some kind soul
 > > jog my memory for me please?
 > > 

Here's a somewhat bizarre version, posted mainly for entertainment
value:


{0:b, 1:c}[not a]

or if you don't like the truth-values being inverted that way:

{1:b, 0:c}[not not a]

According to the language reference (which I just checked), "not"
always returns either 0 or 1, so it can be used to turn any object
into a boolean value.





  









More information about the Python-list mailing list