Python comparison operators chaining/grouping left to right?

£可爱£侵袭症+ 提交于 2019-11-26 05:37:06

问题


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?


回答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

(edit)

Chaining is left to right, so in a < b < c, the expression a < b is evaluated before b < c, and if a < b is false, 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().

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



来源:https://stackoverflow.com/questions/25753474/python-comparison-operators-chaining-grouping-left-to-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!