views:

143

answers:

3

I'm pretty much new in Python object oriented programming and I have trouble understanding the super() function (new style classes) especially when it comes to multiple inheritance.

For example if you have something like:

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"

What I don't get is: will the Third() class inherit both constructor methods? If yes, then which one will be run with super() and why?

And what if you want to run the other one? I know it has something to do with Python MRO.

+1  A: 

This is known as the Diamond Problem, the page has an entry on Python, but in short, Python will call the superclass's methods from left to right.

monoceres
+6  A: 

This is detailed with a reasonable amount of detail by Guido himself at http://python-history.blogspot.com/2010/06/method-resolution-order.html (including two earlier attempts).

But, briefly: in your example, Third() will call First.__init__. For such simple situations, Python will look for the attribute (in this case, __init__) on the class's parents, left to right. So, if you define

class Third(First, Second):
    ...

Python will look at First, and, if First doesn't have the attribute, at Second.

This situation becomes more complex when inheritance starts crossing paths (say, if First inherited from Second, for instance). 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, child classes first.

So, for instance, if you had:

class First(object):
    def __init__(self):
        print "first"

class Second(First):
    def __init__(self):
        print "second"

class Third(First):
    def __init__(self):
        print "third"

class Fourth(Second, Third):
    def __init__(self):
        super(Fourth, self).__init__()
        print "that's it"

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 a behaviour which might surprise the user.

Edited to add example of an ambiguous MRO:

class First(object):
    def __init__(self):
        print "first"

class Second(First):
    def __init__(self):
        print "second"

class Third(First, Second):
    def __init__(self):
        print "third"

Should Third's MRO be [First, Second] or [Second, First]? There's no obvious expectation, and Python will raise an error:

TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for bases Second, First

rbp
CrazyJugglerDrummer: Thanks for the formatting fixes :)
rbp
Hey thanks that's pretty much what I needed.
Callisto
Cool, that's good to know :)
rbp
This is a great answer. But could you give an example of a how an incoherent MRO comes about?
Edmund
Of course, I've edited the answer to give a simple example.
rbp