Does Python evaluate if's conditions lazily? [duplicate]
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.
Python's laziness can be proved by the following code:
def foo(): print('foo') return Falsedef bar(): print('bar') return Falsefoo() and bar() #Only 'foo' is printed
On the other hand,
foo() or bar()
would cause both 'foo' and 'bar' to be printed.