Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

(no) fast boolean evaluation ?


  • From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina)
  • Subject: (no) fast boolean evaluation ?
  • Date: Thu, 02 Aug 2007 19:52:07 -0300

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