19

The Python documentation for operator precedence states:

Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right — see section Comparisons...)

What does this mean? Specifically:

  1. "Operators in the same box group left to right (except for comparisons...)" -- do comparisons not group left to right?

  2. If comparisons do not group left to right, what do they do instead? Do they "chain" as opposed to "group"?

  3. If comparisons "chain" rather than "group", what is the difference between "chaining" and "grouping"?

  4. What would be some examples to demonstrate that the comparison operators chain from left to right rather than from right to left?

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125

2 Answers2

45

Grouping (this is what non-comparison operators do):

a + b + c   means   (a + b) + c

Chaining (this is what comparison operators do):

a < b < c   means   (a < b) and (b < c)

Grouping left to right (this is the way things are grouped):

5 - 2 - 1   means   (5 - 2) - 1 == 2

as opposed to grouping right to left (this would produce a different result):

5 - (2 - 1) == 4

Chaining left to right

Chaining is left to right, so in a < b < c, the expression a < b is evaluated before b < c, and if a < b is falsey, b < c is not evaluated.

(2 < 1 < f()) gives the value False without calling the function f, because 2 < 1 evaluates to false, so the second comparison does not need to be performed.

f() > 1 > g() calls f() in order to evaluate the first comparison, and depending on the result, it might or might not need to evaluate the second condition, which requires calling g().

NB. Each operand is evaluated at most once. So in the expression 1 < f() < 2, the function f() is only called once, and the value it gives is used in both comparisons (if necessary).

https://en.wikipedia.org/wiki/Short-circuit_evaluation

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    Thanks @khelwood, that's helpful! That's a good example showing how grouping left-to-right differs from right-to-left. I'm looking for a similar chaining example to show how chaining left-to-right differs from right-to-left. If it chained right-to-left, then would `a < b < c` mean `(b < c) and (a < b)` ? – Rob Bednark Sep 09 '14 at 21:10
  • `True > False == False` this code give `True` as result. but if you apply Chaining left to right, it should give `False`. – surya singh Oct 17 '17 at 09:16
  • @suryasingh `True > False` is true. `False==False` is true. So `True > False == False` is true. – khelwood Oct 17 '17 at 10:10
  • @khelwood got it. i misunderstand it. i though it is `True > False` gives True than True == False will give False. Now got it. Thank you everyone. – surya singh Oct 17 '17 at 10:18
  • Something is wrong here `(True > False)` → `True`, so you have `True == False` → `False`. Alternatively, if it were: `True > (False == False)` → `True > True` → `False`, so in both cases it should be `False`, never `True` – juanfal Oct 24 '17 at 18:36
  • @juanfal No, because comparison operators **chain**, as described in the answer. – khelwood Oct 24 '17 at 21:26
1

In fact, the chain behavior is not so obvious.

a == b == c

although one would expect this to be converted to

a == b and b == c

it is in fact converted into somthing similar to

b == c if a == b else False

which is a bit confusing if one tries to override the behavior of the comparison operators and chain them.

PhMota
  • 163
  • 1
  • 6
  • More like `b == c if a == b else a == b` (except that `a==b` is only evaluated once). But that is also what `a==b and b==c` gives you, so they are equivalent. – khelwood Jan 26 '22 at 09:37