Reputation: 543
If I run the following Python 3.7 code
a=None
b=None
a==b
>> True
b is not None
>> False
True is not None
>> True
a==b is not None
>> False
since a==b is True, this makes me understand this code evaluates as A==B where A=a and B=b is not None
However, if I run the following code
a = datetime(2020,1,1)
b = datetime(2020,1,1)
a==True
>> False
b is not None
>> True
a==b is not None
>> True
it contradicts the above. So I'm puzzled a bit with how all this is evaluated.
Thanks for your help!
Upvotes: 0
Views: 72
Reputation: 71
you are seeing operator chaining in your example,
a==b is not None
is equivalent to:
a==b and b is not None
Thus the result.
If you want to compare a==b
, use an atomic expression:
(a==b) is not None
See 6.10. Comparisons for details.
In short, a op1 b op2 c ... y opN z
is equivalent to a op1 b and b op2 c and ... y opN z
, except that each expression is evaluated at most once.
Upvotes: 1