There is a magic method called __ne__
in Python which is triggered on objects !=
comparison.
Example:
class A(object):
def __init__(self, a):
self.a = a
def __ne__(self, other):
return self.a != other.a
A(3) != A(3) # produces False
A(3) != A(2) # produces True
The Question:
What happens under the hood if __ne__
is not defined?
Note: In python 3.x !=
comparison is defined to be invert of whatever __eq__
returns.
I thought that object ids are compared, in this case, assuming that we do not have singleton, all !=
comparisons would have to return True
. But apparently the same code on different environments was producing different results, so I guess, there is something else which is compared instead of object ids.