Hasse1987
Hasse1987

Reputation: 333

are compounded equality tests short circuited in python?

Does python short circuit a boolean like a==b==c, i.e., if a==b is false, then the second equality isn't evaluated? Is the compound equality just syntactic sugar for a==b and b==c?

Upvotes: 1

Views: 52

Answers (2)

user2357112
user2357112

Reputation: 280237

Yup. From the docs:

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

This applies to any chained comparison, regardless of the chosen comparison operators.

Upvotes: 5

Numilani
Numilani

Reputation: 366

From the Python docs:

Comparisons can be chained arbitrarily; for example, x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

Upvotes: 3

Related Questions