How does super() work with multiple inheritance? For example, given:
class First(object):
def __init__(self):
print "first"
class Second(object):
def __init__(self):
print "second"
class Third(First, Second):
def __init__(self):
super(Third, self).__init__()
print "that's it"
Which parent method of Third does super().__init__ refer to? Can I choose which runs?
I know it has something to do with method resolution order (MRO).
This is detailed with a reasonable amount of detail by Guido himself in his blog post Method Resolution Order (including two earlier attempts).
In your example,
Third()will callFirst.__init__. Python looks for each attribute in the class’s parents as they are listed left to right. In this case, we are looking for__init__. So, if you definePython will start by looking at
First, and, ifFirstdoesn’t have the attribute, then it will look atSecond.This situation becomes more complex when inheritance starts crossing paths (for example if
Firstinherited fromSecond). Read the link above for more details, but, in a nutshell, Python will try to maintain the order in which each class appears on the inheritance list, starting with the child class itself.So, for instance, if you had:
the MRO would be
[Fourth, Second, Third, First].By the way: if Python cannot find a coherent method resolution order, it’ll raise an exception, instead of falling back to behavior which might surprise the user.
Example of an ambiguous MRO:
Should
Third‘s MRO be[First, Second]or[Second, First]? There’s no obvious expectation, and Python will raise an error:Why do the examples above lack
super()calls? The point of the examples is to show how the MRO is constructed. They are not intended to print"first\nsecond\third"or whatever. You can – and should, of course, play around with the example, addsuper()calls, see what happens, and gain a deeper understanding of Python’s inheritance model. But my goal here is to keep it simple and show how the MRO is built. And it is built as I explained: