(no) fast boolean evaluation ?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Aug 2 18:52:07 EDT 2007


En Thu, 02 Aug 2007 18:47:49 -0300, Stef Mientki  
<S.Mientki-nospam at mailbox.kun.nl> escribió:

> I discovered that boolean evaluation in Python is done "fast"
> (as soon as the condition is ok, the rest of the expression is ignored).
>
> Is this standard behavior or is there a compiler switch to turn it  
> on/off ?

The exact behavior is defined in the Language Reference  
<http://docs.python.org/ref/Booleans.html>

"The expression x and y first evaluates x; if x is false, its value is  
returned; otherwise, y is evaluated and the resulting value is returned.
The expression x or y first evaluates x; if x is true, its value is  
returned; otherwise, y is evaluated and the resulting value is returned.
Note that neither and nor or restrict the value and type they return to  
False and True, but rather return the last evaluated argument. This is  
sometimes useful, e.g., if s is a string that should be replaced by a  
default value if it is empty, the expression s or 'foo' yields the desired  
value."

Tutorial section 5.7 say the same thing in a colloquial way.

-- 
Gabriel Genellina




More information about the Python-list mailing list