Equivalent to (a ? b : c) ?

Skip Montanaro skip at mojam.com
Mon Dec 20 12:25:07 EST 1999


Regarding C's

    (a?b:c)

vs. various Python approximations like

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

or

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

or

    a and b or c

or

    def ternaryif(a, b, c):
	if a: return b
	return c

folks need to remember that in C's construct, only one of b or c are ever
evaluated, depending only on the value of a.  Of the options I've seen
posted this morning, only the rather obtuse

    (a and (b,) or (c,))[0]

meets that criterion.

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
847-971-7098   | Python: Programming the way Guido indented...




More information about the Python-list mailing list