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 45 times
1
Hasse1987
- 233
- 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 <= zis equivalent tox < yandy <= z, except thatyis evaluated only once (but in both caseszis not evaluated at all whenx < yis found to be false).
This applies to any chained comparison, regardless of the chosen comparison operators.
Arn
- 1,780
- 11
- 25
user2357112
- 235,058
- 25
- 372
- 444
3
From the Python docs:
Comparisons can be chained arbitrarily; for example,
x < y <= zis equivalent tox < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all whenx < yis found to be false).
Numilani
- 324
- 3
- 14