Edit: I am on Python 3.
I have a class X
that extends both Y
and Z
:
class X(Y, Z):
def __ini
This depends entirely on whether Y
and Z
were designed to work cooperatively with multiple inheritance. If they are, then you should be able to just use super
:
class X(Y, Z):
def __init__(self):
super().__init__()
When I say that Y
and Z
need to be designed to work cooperatively with multiple inheritance, I mean that they must also call super in their __init__
methods:
class Y:
def __init__(self):
print('Y.__init__')
super().__init__()
class Z:
def __init__(self):
print('Z.__init__')
super().__init__()
They should also have some strategy for handling different arguments since (particularly the constructor) of a class frequently differ in which keyword arguments that they accept. At this point, it's worth reading Super considered super! and Super considered harmful to understand some common idioms and pitfalls regarding super.
If you don't know whether Y
and Z
were designed for cooperative multiple inheritance, then the safest recourse is to assume that they weren't.
If the classes aren't designed to work with cooperative multiple inheritance, then you need to start stepping lightly (you're in dangerous waters). You can call each __init__
individually:
class X(Y, Z):
def __init__(self):
Y.__init__(self)
Z.__init__(self)
But really it's probably better to not use multiple inheritance and instead choose a different paradigm (e.g. composition).