If I have a function which returns a boolean value based on two or more conditions, does Python check every condition?
More specifically, this is a theoretical function:
def f(x, y):
return x < y and f2(x, y) == 1
If f2 takes a while to execute, should I change f to this?
def f(x, y):
if x >= y: return False
return f2(x, y) == 1
Will Python automatically return False if x is greater than or equal to y because of the and up ahead?
Which is the faster of the two and why?
My question would similarly apply also with or statements, if the first condition is true does it keep evaluating the next conditions?
From the docs: “The Boolean operators
andandorare so-called short-circuit operators: their arguments are evaluated from left to right, and evaluation stops as soon as the outcome is determined.”