Python 为什么 `True == False is False` 评估为 False?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17202207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:45:17  来源:igfitidea点击:

Why does `True == False is False` evaluate to False?

python

提问by raylu

I get some rather unexpected behavior on an expression that works with ==but not with is:

我在一个适用于==但不适用于 的表达式上得到了一些相当意外的行为is

>>> (True == False) is False
True
>>> True == (False is False)
True
>>> True == False is False
False
>>> id(True)
8978640
>>> id(False)
8978192
>>> id(True == False)
8978192
>>> id(False is False)
8978640

采纳答案by jorgeca

Because in fact that's a chained comparison, so

因为实际上这是一个链式比较,所以

True == False is False

is equivalent to

相当于

(True == False) and (False is False)

This can be surprising in this case, but lets you write 1 <= x < 4unlike in other languages like C.

在这种情况下,这可能令人惊讶,但可以让您编写1 <= x < 4与 C 等其他语言不同的代码。

回答by BrenBarn

True == False is Falseis a chained comparison, which means the same as (True == False) and (False is False). Since the first comparison (True==False) is false, the result of the chained comparison is False.

True == False is False是一个链式比较,这意味着与 相同(True == False) and (False is False)。由于第一次比较 ( True==False) 为假,因此链式比较的结果为假。

回答by SylvainD

From the documentation:

文档

5.9. Comparisons

Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics:

5.9. 比较

与 C 不同,Python 中的所有比较操作都具有相同的优先级,低于任何算术、移位或按位运算的优先级。同样与 C 不同的是,像 a < b < c 这样的表达式具有数学中的常规解释:

comparison    ::=  or_expr ( comp_operator or_expr )*
comp_operator ::=  "<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
                   | "is" ["not"] | ["not"] "in"

回答by Ashwini Chaudhary

From the docs:

文档

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

x < y <= z 等价于 x < y 和 y <= z,除了 y 只计算一次(但在这两种情况下,当发现 x < y 为假时,根本不计算 z)。

In your case True == False is Falseis equivalent to True == False and False is Falseas the first condition is Falseso it short-circuits and return False.

在您的情况下True == False is False,相当于True == False and False is False第一个条件,False因此它短路并返回False

>>> dis.dis(lambda : True == False is False)
  1           0 LOAD_GLOBAL              0 (True)
              3 LOAD_GLOBAL              1 (False)
              6 DUP_TOP             
              7 ROT_THREE           
              8 COMPARE_OP               2 (==)
             11 JUMP_IF_FALSE_OR_POP    21          <---------this step
             14 LOAD_GLOBAL              1 (False)
             17 COMPARE_OP               8 (is)
             20 RETURN_VALUE        
        >>   21 ROT_TWO             
             22 POP_TOP             
             23 RETURN_VALUE