>>> class Yeah(object):
... def __eq__(self, other):
... return True
...
>>> class Nah(object):
... def __eq__(self, other):
... return False
...
>>> y = Yeah()
>>> n = Nah()
>>> y == n
True
>>> n == y
False
The left guy wins because when python2 sees x == y
it tries x.__eq__(y)
first.
Is there any way to modify Nah
so that he will win both times?
My use-case is making something like this:
class EqualsAnyDatetime(object):
def __eq__(self, other):
return isinstance(other, datetime)
It just works in python3 because real_datetime.__eq__(random_other_thing)
raises NotImplemented
, giving the other side a shot at the comparison. In python2 I can't seem to get the idea working.