Order of evaluation in conditionals

Paul Hankin paul.hankin at gmail.com
Mon Feb 25 16:25:10 EST 2008


On Feb 25, 9:18 pm, Karlo Lozovina <_kar... at mosor.net> wrote:
> Hi all! Here is what's bugging me: let's say I have this code:
>
> if (<condition1>) and (<condition2>) and (<condition3>):
>   do_something()
>
> Is there a guarantee that Python will evaluate those conditions in order (1,
> 2, 3)? I know I can write that as a nested if, and avoid the problem
> altogether, but now I'm curious about this ;).

Did you try to find the answer to your question in the python
reference manual? The relevant page is http://docs.python.org/ref/Booleans.html

To quote it:
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.

--
Paul Hankin




More information about the Python-list mailing list