Is there a way to test whether a costume class had explicitly defined an attribute like __gt__
? To put things in context consider these two classes:
class myclass1:
def __init__(self, val):
self.val = val
def __gt__(self, other):
if type(other) is int:
return self.val > other
else:
return self.val > other.val
class myclass2:
def __init__(self, val):
self.val = val
Since I've defined an inequality attribute only for myclass1
and not for myclass2
calling
x1 = myclass1(5); x2 = myclass2(2)
x1 > x2
x2 < x1
will use myclass1.__gt__
in both cases. Had I defined myclass2.__lt__
, the last line would have invoked it. But I didn't. So x1
's __gt__
takes hold in both calls. I think I understand this (but comments are welcomed).
So my question: Is there a way to know which inequalities had been explicitly defined for a custom class? Because
hasattr(x2, '__gt__')
returns True
anyway.