something like "if" that returns a value?

Terry Reedy tjreedy at udel.edu
Tue Nov 12 02:49:00 EST 2002


"Paul Rudin" <paul.rudin at ntlworld.com> wrote in message
news:m3bs4vmefm.fsf at localhost.localdomain...
> >>>>> "John" == John Roth <johnroth at ameritech.net> writes:
>
>
>  John> The 'and' 'or' hack is the cannonical way of doing this. It
>  John> short circuits properly, and it returns the value of the
>  John> expression that was evaluated.
>
> But it doesn't always short circuit properly, does it?  x and y or
z,
> will evaluate both of y and z in some circumstances.

Only if y evaluates as False.  If that is a possibility, one must
elaborate above as
(x and (y,) or (z,))[0]
(False,) is tuple with one element and not empty and therefore not
null.
In many practical situations, this is demonstrably not needed.

Terry J. Reedy





More information about the Python-list mailing list