【发布时间】:2020-10-28 02:13:42
【问题描述】:
如果我运行以下 Python 3.7 代码
a=None
b=None
a==b
>> True
b is not None
>> False
True is not None
>> True
a==b is not None
>> False
因为 a==b 是 True,这让我明白这段代码的计算结果为 A==B,其中 A=a 和 B=b 不是 None
但是,如果我运行以下代码
a = datetime(2020,1,1)
b = datetime(2020,1,1)
a==True
>> False
b is not None
>> True
a==b is not None
>> True
它与上述矛盾。所以我对如何评估这一切感到有点困惑。
感谢您的帮助!
【问题讨论】:
-
第一个示例
a==b is not None扩展为a==b and b is not None=>True and False=>False。第二个例子扩展a==b and b is not None=>True and True=>True -
表达式的扩展是为什么 Python 允许您编写
a < b < c,它变成a < b and b < c而大多数其他语言只是从字面上尝试将a < b布尔值与 c 进行比较并得到废话。
标签: python operators python-3.7