【发布时间】:2020-08-14 15:38:47
【问题描述】:
下一段代码是如何工作的?
a = None
b = None
print(a == b) # True
print(a == b is None) # also True
第一个打印很明显,None == None,但是,这是我不明白的第二个打印。我可以想到两种解释方式:
-
(a == b) is None,但这应该(并且确实)给我们 False,因为 True 不是 None -
a == (b is None),这也是 False,因为b is None是 True 而None != True
所以,我无法想象 Python 是如何在此语句中获得 True 的。
【问题讨论】:
-
Python 中的比较链。
a==b is None表示(a==b) and (b is None)docs.python.org/3/reference/expressions.html#comparisons
标签: python compare interpreter nonetype