Python "is not True" 和 "is False" 一样吗?

Python "is not True" same as "is False"?

我很好奇 "x is not True" 在 Python 中的优先级。起初,我假设它的意思是“!= True”,但后来我认为 "not True" 肯定比 "is not" 具有更高的优先级。然而以下似乎表明后者:

>>> 4 is not True    
True    
>>> not True    
False     
>>> 4 is False     
False     
>>> 4 is (not True)
False

似乎 Python 将 "is not True" 解释为单个表达式,而不是等同于 "is False".

的 "is (not True)"

我对Python编程并不陌生,但我以前没有深入思考过这个问题。我可以安全地假设 "is not" 本身是一个运算符,它具有比 "not True" 更高的解析优先级吗?

不,不是。 x is not True 对于 任何不是单例对象 True

的值 x 都为真

It seems Python interprets "is not True" as a single expression, rather than "is (not True)" which is equivalent to "is False".

没错。在反汇编的字节码中其实可以看到这个,is not是自己的比较操作

>>> import dis
>>> dis.dis("x is not True")
  1           0 LOAD_NAME                0 (x)
              2 LOAD_CONST               0 (True)
              4 COMPARE_OP               9 (is not)
              6 RETURN_VALUE
>>>

是的,在python中,is not是一个独立的运算符,而不是isnot的组合。 is not 运算符的优先级高于 not 运算符。如果需要,可以在 Python's documentation

中查看

4(不是)Truebool),因为4就是4(int)。

is not是一个运算符,与is相反。

x is not y

表示

not (x is y) 

不是意思

x is (not y)

例如

4 is not False

True(意思是not (4 is False)

4 is (not False)

False(因为 not False 的计算结果为 True,而不是值 4)。