Say I have a multiple inheritance scenario:
class A(object):
# code for A here
class B(object):
# code for B here
class C(A, B):
def __init__(self):
# What's the right code to write here to ensure
# A.__init__ and B.__init__ get called?
There's two typical approaches to writing C
's __init__
:
- (old-style)
ParentClass.__init__(self)
- (newer-style)
super(DerivedClass, self).__init__()
However, in either case, if the parent classes (A
and B
) don't follow the same convention, then the code will not work correctly (some may be missed, or get called multiple times).
So what's the correct way again? It's easy to say "just be consistent, follow one or the other", but if A
or B
are from a 3rd party library, what then? Is there an approach that can ensure that all parent class constructors get called (and in the correct order, and only once)?
Edit: to see what I mean, if I do:
class A(object):
def __init__(self):
print("Entering A")
super(A, self).__init__()
print("Leaving A")
class B(object):
def __init__(self):
print("Entering B")
super(B, self).__init__()
print("Leaving B")
class C(A, B):
def __init__(self):
print("Entering C")
A.__init__(self)
B.__init__(self)
print("Leaving C")
Then I get:
Entering C
Entering A
Entering B
Leaving B
Leaving A
Entering B
Leaving B
Leaving C
Note that B
's init gets called twice. If I do:
class A(object):
def __init__(self):
print("Entering A")
print("Leaving A")
class B(object):
def __init__(self):
print("Entering B")
super(B, self).__init__()
print("Leaving B")
class C(A, B):
def __init__(self):
print("Entering C")
super(C, self).__init__()
print("Leaving C")
Then I get:
Entering C
Entering A
Leaving A
Leaving C
Note that B
's init never gets called. So it seems that unless I know/control the init's of the classes I inherit from (A
and B
) I cannot make a safe choice for the class I'm writing (C
).
Best Answer
The answer to your question depends on one very important aspect: Are your base classes designed for multiple inheritance?
There are 3 different scenarios:
The base classes are unrelated, standalone classes.
If your base classes are separate entities that are capable of functioning independently and they don't know each other, they're not designed for multiple inheritance. Example:
Important: Notice that neither
Foo
norBar
callssuper().__init__()
! This is why your code didn't work correctly. Because of the way diamond inheritance works in python, classes whose base class isobject
should not callsuper().__init__()
. As you've noticed, doing so would break multiple inheritance because you end up calling another class's__init__
rather thanobject.__init__()
. (Disclaimer: Avoidingsuper().__init__()
inobject
-subclasses is my personal recommendation and by no means an agreed-upon consensus in the python community. Some people prefer to usesuper
in every class, arguing that you can always write an adapter if the class doesn't behave as you expect.)This also means that you should never write a class that inherits from
object
and doesn't have an__init__
method. Not defining a__init__
method at all has the same effect as callingsuper().__init__()
. If your class inherits directly fromobject
, make sure to add an empty constructor like so:Anyway, in this situation, you will have to call each parent constructor manually. There are two ways to do this:
Without
super
With
super
Each of these two methods has its own advantages and disadvantages. If you use
super
, your class will support dependency injection. On the other hand, it's easier to make mistakes. For example if you change the order ofFoo
andBar
(likeclass FooBar(Bar, Foo)
), you'd have to update thesuper
calls to match. Withoutsuper
you don't have to worry about this, and the code is much more readable.One of the classes is a mixin.
A mixin is a class that's designed to be used with multiple inheritance. This means we don't have to call both parent constructors manually, because the mixin will automatically call the 2nd constructor for us. Since we only have to call a single constructor this time, we can do so with
super
to avoid having to hard-code the parent class's name.Example:
The important details here are:
super().__init__()
and passes through any arguments it receives.class FooBar(FooMixin, Bar)
. If the order of the base classes is wrong, the mixin's constructor will never be called.All base classes are designed for cooperative inheritance.
Classes designed for cooperative inheritance are a lot like mixins: They pass through all unused arguments to the next class. Like before, we just have to call
super().__init__()
and all parent constructors will be chain-called.Example:
In this case, the order of the parent classes doesn't matter. We might as well inherit from
CoopBar
first, and the code would still work the same. But that's only true because all arguments are passed as keyword arguments. Using positional arguments would make it easy to get the order of the arguments wrong, so it's customary for cooperative classes to accept only keyword arguments.This is also an exception to the rule I mentioned earlier: Both
CoopFoo
andCoopBar
inherit fromobject
, but they still callsuper().__init__()
. If they didn't, there would be no cooperative inheritance.Bottom line: The correct implementation depends on the classes you're inheriting from.
The constructor is part of a class's public interface. If the class is designed as a mixin or for cooperative inheritance, that must be documented. If the docs don't mention anything of the sort, it's safe to assume that the class isn't designed for cooperative multiple inheritance.