比较python中的两个布尔表达式

In[19]: x = None
In[20]: y = "Something"
In[21]: x is None == y is None
Out[21]: False
In[22]: x is None != y is None ## What's going on here?
Out[22]: False
In[23]: id(x is None)
Out[23]: 505509720
In[24]: id(y is None)
Out[24]: 505509708

为什么Out [22]是假的?他们有不同的ID,所以这不是身份问题….

最佳答案 你的x是无!= y是无是“
chained comparisons”.更典型的例子是3< x

(x is None) and (None != y) and (y is None)

这是假的,因为y是None是假的.

点赞