Order of evaluation in conditionals

Tim Chase python.list at tim.thechases.com
Mon Feb 25 16:31:01 EST 2008


> 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 ;).

Yes, Python does short-circuit evaluation, from left-to-right

http://docs.python.org/tut/node7.html#SECTION007700000000000000000

That means that if <condition1> returns false, condition[2|3] 
will not be evaluated (and similarly, if condition2 returns 
false, condition3 won't be evaluated).

-tkc






More information about the Python-list mailing list