Builtn super() function. How to use it with multiple inheritance? And why should I use it at all?

Raymond Hettinger raymond.hettinger at gmail.com
Sat Jul 24 04:20:40 EDT 2010


On Jul 24, 12:47 am, Lacrima <lacrima.ma... at gmail.com> wrote:
> Hi!
>
> I have two super classes:
>
> class SuperClass1(object):
>     def __init__(self, word):
>         print word
>
> class SuperClass2(object):
>     def __init__(self, word, word2):
>         print word, word2
>
> Also I have subclass of these classes:
>
> class SubClass(SuperClass1, SuperClass2):
>     def __init__(self):
>         pass
>
> I have two questions.
> 1) Inside __init__ of SubClass how can I firstly call __init__ of
> SuperClass1, and then __init__ of SuperClass2, using builtin super()
> function.

I would write it like this:


class SuperClass1(object):
    def __init__(self, **kwds):
        word = kwds.pop('word')
        print word
        super(SuperClass1, self).__init__(**kwds)

class SuperClass2(object):
    def __init__(self, **kwds):
        word1 = kwds.pop('word1')
        word2 = kwds.pop('word2')
        print word1, word2
        super(SuperClass2, self).__init__(**kwds)

class SubClass(SuperClass1, SuperClass2):
    def __init__(self, **kwds):
        super(SubClass, self).__init__(**kwds)

SubClass(word='Python', word1='Hello', word2='World')




> 2) Why should I use super() at all, if it is very easy to call methods
> of super class like this:
> class SubClass(SuperClass1, SuperClass2):
>     def __init__(self):
>         SuperClass1.__init__(self, 'Python')
>         SuperClass2.__init__(self, 'Hello', 'world')

That works just fine in this case.
The challenge arises in "diamond diagrams"
such as A->B  A->C  B->D  C->D where both B and C
are written independently of D and both need to call
A's __init__ but that method should only be called
once (not once by B and again by C).

In that case, the super() pattern shown above will
let each parent's method be called exactly once
and guarantee that parents are called before grandparents
and guarantee that the left-to-right ordering of multiple
bases is respected.


Raymond




More information about the Python-list mailing list