I think in Python ==
should check for equality, and is
for identity.
From a logical point of view, I think not (a == b)
should be equal to a != b
. Do I overlook something?
Two things came to my mind: (1) Classes which implement __ne__
and __eq__
in a bad way (2) Something with floats / float('nan')
. But I couldn't find any examples where the two expressions would not be equivalent. Their byte code isn't:
>>> def a(compare):
... return not compare == "in"
>>> def b(compare):
... return compare != "in"
>>> import dis
>>> dis.dis(a)
2 0 LOAD_FAST 0 (compare)
2 LOAD_CONST 1 ('in')
4 COMPARE_OP 2 (==)
6 UNARY_NOT
8 RETURN_VALUE
>>> dis.dis(b)
2 0 LOAD_FAST 0 (compare)
2 LOAD_CONST 1 ('in')
4 COMPARE_OP 3 (!=)
6 RETURN_VALUE