Python comparison operators chaining/grouping left to right?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x