I have a base class with two classes that are derived from it. I want the methods of the base classes to behave differently depending on whether the arguments are of the same type as the derived class, or only instances of the base class but a different type. This is the current implementation:
class MyBase:
def __init__(self, foo: int):
self.foo = foo
def __eq__(self, other):
return self.foo == other.foo
class MyDerived_1(MyBase):
def __init__(self, foo: int, bar: int):
super().__init__(foo)
self.bar = bar
class MyDerived_2(MyBase):
def __init__(self, foo: int, bar: int):
super().__init__(foo)
self.bar = bar
def __eq__(self, other):
if type(other) == type(self):
return self.bar == other.bar
elif isinstance(other, MyBase):
return super().__eq__(other)
else:
return False
In the fourth last line I have to reference MyBase explicitly. Perhaps this is fine but my understanding is that a main point of the "super" keyword is that it should allow you to change the base class without having to re-write anything in the class. So I.e. a potential issue with this solution is that if MyBase is changed then init will be fine because it calls "super", but eq will not update its behaviour.
So I attempted replacing "MyBase" with "type(super)" or "type(super())", but these do not reference the super class, they reference the class of the object "super".
Note that this questions differs from:
Get parent class name? Get defining class of unbound method object in Python 3 etc.
Because they are looking for the parent classes once the object has been initialised.
I guess that I should be able to find the super class by running up the MRO. But this seems like a bad solution given that I'm not looking for the whole inheritance tree, I just want to know the type of the super class.
Is there a way to pull that information out of "super"?