Equivalent to (a ? b : c) ?
Alex Martelli
Alex.Martelli at think3.com
Mon Dec 20 11:49:55 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?
>
For the general case I think it was:
((a and (b,)) or (c,))[0]
where the tuple trickery is just in case b is false; if you
know b can't be false,
(a and b) or c
seems more readable. I think the parentheses can also
be dispensed with, but I'm not conversant enough with
the precedence to dare do that:-).
Alex
More information about the Python-list
mailing list