1

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?

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 Answers2

5

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.

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 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).

Numilani
  • 346
  • 4
  • 16