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
?
Asked
Active
Viewed 51 times
1

Hasse1987
- 243
- 1
- 9
-
Does this answer your question? [Does Python support short-circuiting?](https://stackoverflow.com/questions/2580136/does-python-support-short-circuiting) – Numilani Dec 12 '19 at 20:41
-
@Ambrosia only if the answer to my second question about syntactic sugar is "yes" – Hasse1987 Dec 12 '19 at 20:42
2 Answers
5
Yup. From the docs:
Comparisons can be chained arbitrarily, e.g.,
x < y <= z
is equivalent tox < y
andy <= z
, except thaty
is evaluated only once (but in both casesz
is not evaluated at all whenx < y
is found to be false).
This applies to any chained comparison, regardless of the chosen comparison operators.

Arn
- 1,898
- 12
- 26

user2357112
- 260,549
- 28
- 431
- 505
3
From the Python docs:
Comparisons can be chained arbitrarily; for example,
x < y <= z
is equivalent tox < y and y <= z
, except that y is evaluated only once (but in both cases z is not evaluated at all whenx < y
is found to be false).

Numilani
- 346
- 4
- 16