PEP 308: A PEP Writer's Experience - PRO

Erik Max Francis max at alcyone.com
Sun Feb 9 18:37:49 EST 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]
f
>>> (False and [f()] or [g()])[0]
g

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

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ 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 / http://www.alcyone.com/max/projects/cards/buh.html
 The official rules to the betting card game, Buh.




More information about the Python-list mailing list