Python 201: What is super?

The Python programming language added the super() type back in version 2.2. For some reason, it’s still a topic that a lot of beginners don’t understand. One of my readers recently asked me about it and since I don’t really use it, I decided to do some research in the hopes of understanding its usage myself so I could explain what super is and why you would use it. We’ll spend some time looking at various people’s definitions of super and then look at some examples to try to figure this out.

What is super?

Here is what the official Python documentation has to say about super: Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.

A couple of years ago, Cody Precord released a wxPython Cookbook in which he used super all the time. That brought up the discussion of super on the wxPython mailing list for several weeks. Let’s take a look at some of those threads.

Robin Dunn, creator of wxPython, stated the following: In most cases there isn’t any difference. In cases where you have multiple inheritance, the super() helps to ensure that the proper
method resolution order (MRO) is followed when moving up the inheritance tree.
He then linked to StackOverflow.

In another thread, Tim Roberts (a very experienced Python programmer) had the following to say:

It is a “shortcut” to allow you to access the base class of a derived class, without having to know or type the base class name. For example:

class This_is_a_very_long_class_name(object):
    def __init__(self):
        pass

class Derived(This_is_a_very_long_class_name):
    def __init__(self):
        super(Derived,self).__init__()   #1
        This_is_a_very_long_class_name.__init__(self)    #2


Those last two lines are two ways of spelling the same thing. Besides just the spelling, this also allows you to change the base class without having to go through all of your code and replace the base class name. C++ programmers often do this with a typedef in their derived classes.

So what I get from these 2 statements is that super is important when you’re doing multiple inheritance, but otherwise it doesn’t really matter whether you use it or not. If you scroll down through that second thread, you’ll see a couple of examples from both Robin Dunn and Tim Roberts that help illustrate when using super can be helpful. Let’s take a look at Mr. Dunn’s example.

Note: The following code uses Python 2.x syntax for super. In Python 3, you don’t need to pass the class name or even self!

class A(object):
    def foo(self):
        print 'A'

class B(A):
    def foo(self):
        print 'B'
        super(B, self).foo()

class C(A):
    def foo(self):
        print 'C'
        super(C, self).foo()

class D(B,C):
    def foo(self):
        print 'D'
        super(D, self).foo()

d = D()
d.foo() 

If you run this code, you will get the letters “DBCA” (one letter per line) as output. As Robin Dunn points out in the thread, “A” is only printed once even through two classes derive from it. This is due to the method resolution order (MRO) that is inherent in the new style classes of Python. You can check out the MRO by adding print D.__mro__ underneath the super call in class D’s foo function (note: this only works if the base is derived from object). This StackOverflow entry explains it in more detail. If you want a good read on the subject of MRO, I would recommend the following abstract on Python.org: http://www.python.org/download/releases/2.3/mro/.

Now you may be thinking that this example is not only abstract, but not of much use and you would be quite right. Which is why it is worth doing some additional digging online to find other examples. Fortunately, I recalled seeing an article by Raymond Hettinger on super a couple of years ago. He is a core Python developer and speaks at PyCon regularly. Anyway, his article gives several good real-life examples of using super to add new features to sub-classes. I highly recommend checking out his article as it goes a long ways towards explaining super in an applicable way. Even the Python documentation for super() links to his article!

Here’s a pretty good list on the topic:

9 thoughts on “Python 201: What is super?”

  1. Usage of super in __init__ is a whole topic on it’s own !
    Normally one would say super().__init__ shall be called in every init, to give chance to class in class hierarchy to initialize, but the problem is that due to MRO, you can’t guess which arguments does the __init__ method of the class next to you in MRO expects, and you do not give a chance to the multiple inheriting class to solve this.
    So multiple inheritance is quite hard, and sometimes, keeping things simple requires not to use super() ! So the multiple inheriting class can decide which method to call, when and with which arguments.

  2. Yeah, I’ve heard that the way super() is used in __init__ in regards to multiple inheritance is difficult to follow unless you understand the algorithm super uses. I think I have only used multiple inheritance when I have used wxPython’s mixin classes.

  3. Excellent post. I’ve been using super with Django testing, but just copying examples. I had an idea of what it was doing, but I didn’t fully understand it. Thanks.

    I love the lessons on this site! You are a fantastic teacher.

  4. “It is always darkest just before the Day dawneth.” I ran into this
    abstruse topic (at least to me, a greenhorn python learner at codeacademy) and was ready to throw in the towel. (wow, what a cliché ridden intro!) And forgive me if I add, “You saved my bacon.” Sincere thanks for taking the time to cover this subject in such comprehensive and understandable detail. Now day seems to be breaking.

  5. For clarification, we might preface the docs’ definition with the spec: `super([type[, object-or-type]])` .
    When I read it without that, I was bewildered!
    “Return a proxy object that delegates method calls to a parent or sibling class of type.” Of class type? Aaaaaaah, okay. 🙂

  6. Pingback: Python 201 - super - The Mouse Vs. The Python

Comments are closed.