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
是一个独立的运算符,而不是is
和not
的组合。 is not
运算符的优先级高于 not
运算符。如果需要,可以在 Python's documentation
中查看
4(不是)True
(bool
),因为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
)。
我很好奇 "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
是一个独立的运算符,而不是is
和not
的组合。 is not
运算符的优先级高于 not
运算符。如果需要,可以在 Python's documentation
4(不是)True
(bool
),因为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
)。