I have two classes A
and B
and both have a method get_type()
that returns a string and which is called in the __init__()
of both classes to set an instance attribute self.type
. Now there's a child class that inherits from both A
and B
and in it's __init__()
it takes an instance of either A
or B
as an argument. Depending on whether it's instantiated with an instance of A
or B
, I call the corresponding __init__()
of the parent. Presumably because of the lookup order of multiple inheritance, always the get_type()
method of the A
parent is called, even if C
is instantiated with an instance of B
. See code below:
class A:
def __init__(self):
self.type = self.get_type()
def get_type(self):
return 'A'
class B:
def __init__(self):
self.type = self.get_type()
def get_type(self):
return 'B'
class C(A, B):
def __init__(self, instance):
if isinstance(instance, A):
A.__init__(self)
print('Inheriting from A')
else:
B.__init__(self)
print('Inheriting from B')
a = A()
b = B()
c = C(b)
print(a.type, b.type, c.type)
>>> Inheriting from B
>>> A B A
So I want that c.type
is 'B' since it inherited from the B
class. Can I maybe use the super()
function to get the behaviour I want?