Is `a<b<c` valid python?

后端 未结 2 1162
失恋的感觉
失恋的感觉 2020-12-03 07:12

I was curious to see if I could use this a as a conditional without using the standard a. So I tried it out and my test r

相关标签:
2条回答
  • 2020-12-03 08:05

    Python chains relational operators "naturally". Note that Python's relational operators include in and is (and their negatives), which can lead to some surprising results when mixing them with the symbolic relational operators.

    0 讨论(0)
  • 2020-12-03 08:12

    This is documented here.

    Formally, if a, b, c, ..., y, z are expressions and op1, op2, ..., opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.

    And, as an example,

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

    0 讨论(0)
提交回复
热议问题