Does Python evaluate if's conditions lazily?

ogama8 picture ogama8 · Dec 19, 2012 · Viewed 33k times · Source

For example, if I have the following statement:

if( foo1 or foo2)
    ...
    ...

if foo1 is true, will python check the condition of foo2?

Answer

unutbu picture unutbu · Dec 19, 2012

Yes, Python evaluates boolean conditions lazily.

The docs say,

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.