|
发表于 2017-11-3 13:17:03
|
显示全部楼层
本楼为最佳答案
3>2>2
等价于
3>2 and 2>2
等价于
(3>2) and (2>2)
这就是所谓的“链式比较”。
这里有介绍:
- https://docs.python.org/3.6/reference/expressions.html
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).
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.
Note that a op1 b op2 c doesn’t imply any kind of comparison between a and c, so that, e.g., x < y > z is perfectly legal (though perhaps not pretty).
- https://stackoverflow.com/questions/101268/hidden-features-of-python
- https://stackoverflow.com/questions/101268/hidden-features-of-python#101945 |
|