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
Why is Out[22] false? They have different ids, so it's not an identity problem....
Chained expressions are evaluated from left to right, besides, comparisons is and != have the same precedence, so that your expression is evaluated as:
(x is None) and (None!= y) and (y is None)
#---True----|------True-----|--- False---|
#-----------True------------|
#------------------False-----------------|
To change the order of evaluation, you should put some parens:
>>> (x is None) != (y is None)
True
Also note that the first expression x is None == y is None was a fluke, or rather the red herring, as you'll get the same results if you place some parens in the required positions. It's probably why you assumed the order should start with the is first, then the != in the second case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With