PEP 308: Alternative conditional operator forms

Tim Peters tim.one at comcast.net
Mon Feb 10 12:44:57 EST 2003


[holger krekel]
>> No, that's not a short-circuiting form.

> which is rarely needed.  Tim Peters <wink> said that with the hundred
> cases of the beloved "x and y or z" idiom in the 150+ modules of
> Python it's needed exactly 0 (zero) times.

Nope.  I said the relative handful of examples I included didn't need
short-circuiting, and that it was rarely needed.  In some cases it is, like

    SuiteInfoBase.__init__(self, tree and tree[-1] or None)

but I didn't happen to post that one.  Another is

    iptfile = args and args[0] or "Python/graminit.c"

In other cases it can be merely more efficient to short-circuit, like

     x[1][0] and '" "'.join(x[1]) or ''

The unadorned and/or idiom does short-circuit, so none of the above can be
an issue for it.

What I found much less use for (none in the std library, whether posted or
not) was cause to get excited about the "but if the middle expression is
false and the condition is true, the last expression gets evaluated anyway"
insecurity.  Most uses were slinging literal strings and module constants,
where "but that can't happen" was obvious, and the idea that the middle
expression "might be false" ignores that, e.g., no, the literal "yes" is not
false.

In the end, I didn't find any code in the std library I'd change if the PEP
were to be accepted.  I didn't study every line, though.






More information about the Python-list mailing list