78
votes

For example, if I have the following statement:

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

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

7
My usual tip for keywords, if -- like me -- you're too lazy to load the official docs: type help("or") at the interpreter console. In this case, read the fourth paragraph. - DSM
Python's behavior here has nothing to do with "if" and everything to do with "or." - jwodder
There are some modules that implement lazy evaluation in Python, which may be what you are looking for. - Anderson Green
Technically, python short-circuits and then double-evaluates the result of a boolean operator, if it is later used an actual boolean... unless it is directly in an if statement ... which is privileged (more than not or bool()), and so it evaluates them once. The double-evaluation depends on the complexity of the operation. This is counter intuitive, but there is proof here: gist.github.com/earonesty/08e9cbe083a5e0583feb8a34cc538010 - Erik Aronesty

7 Answers

100
votes

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.

53
votes

and or is lazy

& | is not lazy

21
votes

Python's laziness can be proved by the following code:

def foo():
    print('foo')
    return False

def bar():
    print('bar')
    return False

foo() and bar()         #Only 'foo' is printed

On the other hand,

foo() or bar()

would cause both 'foo' and 'bar' to be printed.

6
votes

This isn't technically lazy evaluation, it's short-circuit boolean expressions.

Lazy evaluation has a somewhat different connotation. For example, true lazy evaluation would likely allow this

def foo(arg) :
    print "Couldn't care less"

foo([][0])

But Python doesn't.

Python is also nice in that it "echos" it's boolean arguments. For example, an or condition returns either it's first "truthy" argument or the last argument (if all arguments are "falsey"). An and condition does the inverse.

So "echo argument" booleans means

2 and [] and 1

evaluates to [], and

[] or 1 or 2

evaluates to 1

3
votes

A short demo would be to compare the time difference between

all(xrange(1,1000000000))

and

any(xrange(1,1000000000))

The all() has to check every single value, whilst the any() can give up after the first True has been found. The xrange, being a generator, therefore also gives up generating things as soon as the evaluator is done. For this reason, the all will consume large amounts of RAM and take ages, whilst the any will use just a few bytes and return instantaneously.

2
votes

Yes, Python evaluates lazily, so foo2 will not be checked.

I use this all the time for grabbing items from dictionary-like objects if I don't know if the key exists:

if 'key' in mydict and mydict['key'] == 'heyyo!':
    do_stuff()

See @unutbu's answer for a fuller explanation.

1
votes

It is really the or part that is short circuited:

>>> 1 or 1/0  #also 0 and 1/0
1
>>> 0 or 1/0  #also 1 and 1/0

Traceback (most recent call last):
  File "<pyshell#1240>", line 1, in <module>
    0 or 1/0
ZeroDivisionError: integer division or modulo by zero