>>> 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 , he first tries x.__eq__(y) .
Is there a way to change Nah so that he wins both times?
My use case does 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 when comparing. In python2, I cannot get this idea to work.
source share