What does it mean that Python comparison operators chain/group left to right?
Asked Answered
A

2

22

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?

Arithmomancy answered 9/9, 2014 at 20:59 Comment(0)
H
53

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

Honolulu answered 9/9, 2014 at 21:2 Comment(5)
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) ?Arithmomancy
True > False == False this code give True as result. but if you apply Chaining left to right, it should give False.Musketry
@suryasingh True > False is true. False==False is true. So True > False == False is true.Honolulu
@Honolulu 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.Musketry
@juanfal No, because comparison operators chain, as described in the answer.Honolulu
W
2

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.

Whalebone answered 23/7, 2020 at 3:2 Comment(4)
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.Honolulu
a == b == c is indeed converted to a == b and b == c so I'm not sure how to interpret the first part of this answer. The behaviour of and and or in python is indeed slightly non-obvious, but chaining itself seems intuitive. For instance, 1 and 3 evaluates to 3; 0 and f(x) evaluates to 0 without performing the f(x) call; 'hello' or f(x) evaluates to 'hello' without calling f(x); [] or False evaluates to []. Assuming a == b and b == c both returns booleans, which is the case for builtin types, then a == b == c also returns a boolean without any surprise.Consistency
Of course if a, b and c belong to a complicated class that redefines ==, for instance a numpy array, then things could get weird. (Perhaps you could edit your answer to add an example of weird behaviour with numpy!!)Consistency
Actually numpy just prudently doesn't allow using boolean operators and and or on arrays, so trying to chain == on numpy arrays safely raises a ValueError: np.array([1, 2, 3]) == np.array([0, 2, 3]) array([False, True, True]) but np.array([1, 2, 3]) == np.array([0, 2, 3]) == np.array([0, 0, 3]) ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). See also NumPy chained comparison with two predicates?Consistency

© 2022 - 2024 — McMap. All rights reserved.