PEP 308: A PEP Writer's Experience - PRO

Erik Max Francis max at
Mon Feb 10 00:37:49 CET 2003

Paul Rubin wrote:

> I'm missing something--x and y both get evaluated in that example,
> so it's only a partial work around?

No, they don't.  Both the "and" and "or" operators implement
short-circuiting, so the single-element lists are only evaluated in the
eventuality that the expression gets that far:

>>> def f(): print 'f'
>>> def g(): print 'g' 
>>> (True and [f()] or [g()])[0]
>>> (False and [f()] or [g()])[0]

Not, of course, that I don't find the syntax abhorrent.

 Erik Max Francis / max at /
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ He who knows how to be poor knows everything.
\__/ Jules Michelet
    Rules for Buh /
 The official rules to the betting card game, Buh.

More information about the Python-list mailing list