for example, I have this class and two instances of it:
class A:
def __init__(self, version):
self.version = version
a1 = A('1.0')
a2 = A('1.0')
because there is no direct way to compare a1
and a2
. I override the __eq__
method of both instances using MethodType.
def compare(self, other):
if not isinstance(other, self.__class__):
raise TypeError
return self.version == other.version
a1.__eq__ = MethodType(compare, a1, a1.__class__)
a2.__eq__ = MethodType(compare, a2, a2.__class__)
However, after all these effort, a1 == a2
still returns False
.
After some research, I get that in order for ==
to be overridden, one must override the __eq__
method for the class, not the instance.
One of the reasons why I don't do this at class level is that doing so would pollute the class, I only produce instances using some functions(not sub-classes) and adding attributes dynamically for comparison.
I want to know if there is a way to override ==
at instance level without change the class.