Python’s super() considered super!

If you aren’t wowed by Python’s super() builtin, then chances are that you don’t really know what it is capable of doing or how to use it effectively.

Much has been written about super() and much of that writing has been a failure. This article seeks to improve on the situation by:

  • providing practical use cases
  • giving a clear mental model of how it works
  • showing the tradecraft for getting it to work every time
  • concrete advice for building classes that use super()
  • solutions to common issues
  • favoring real examples over abstract ABCD diamond diagrams.

The examples for this post are available in both Python 2 syntax and Python 3 syntax.

Using Python 3 syntax, let’s with a basic use case, a subclass for extending one method from one of the builtin classes:

class LoggingDict(dict):
    def __setitem__(self, key, value):
        logging.info('Setting %r to %r' % (key, value))
        return super().__setitem__(key, value)

This class has all the same capabilities as its parent, dict, but it extends the __setitem__ method to make log entries whenever the key is updated. After making a log entry, the method uses super() to delegate the work for actually updating the dictionary with the key/value pair.

Before super() was introduced, we would have hardwired the call with return dict.__setitem__(self, key, value). However, super() is better because it is a computed indirect reference.

One benefit of indirection is that we don’t have to specify the delegate class by name. If you edit the source code to switch the base class to some other mapping, the super() reference will automatically follow. You have a single source of truth:

class LoggingDict(SomeOtherMapping):            # new base class
    def __setitem__(self, key, value):
        logging.info('Setting %r to %r' % (key, value))
        return super().__setitem__(key, value)  # no change needed

In addition to isolating changes, there is another major benefit to computed indirection, one that may not be familiar to people coming from static languages. Since the indirection is computed at runtime, we have freedom to influence that calculation so that the indirection will point to another class.

The calculation depends on both the class where super is called and on the instance’s tree of ancestors. The first component, the class where super is called, is determined by the source code for that class. In our example, super() is called in the LoggingDict.__setitem__ method. That component is fixed. The second and more interesting component is variable (we can create new subclasses with a rich tree of ancestors).

Let’s use this to our advantage to construct a logging ordered dict without modifying our existing classes:

class LoggingOD(LoggingDict, collections.OrderedDict):
    pass

The ancestor tree for our new class is: LoggingOD, LoggingDict, OrderedDict, dict, object. For our purposes, the important result is that OrderedDict was inserted after LoggingDict and before dict! This means that the super() call in LoggingDict.__setitem__ now dispatches the key/value update to OrderedDict instead of dict.

Think about that for a moment. We did not alter the source code for LoggingDict. Instead we built a subclass whose only logic is to compose two existing classes and control their search order.

__________________________________________________________________________________________________________________

Search Order

What I’ve been calling search order or the ancestor tree is officially known as the Method Resolution Order or MRO. It’s easy to view the MRO by printing the __mro__ attribute:

>>> pprint(LoggingOD.__mro__)
(<class '__main__.LoggingOD'>,
 <class '__main__.LoggingDict'>,
 <class 'collections.OrderedDict'>,
 <class 'dict'>,
 <class 'object'>)

If our goal is to create a subclass with an MRO to our liking, we need to know how it is calculated. The basics are simple. The sequence includes the class, its base classes, and the base classes of those bases and so on until reaching object which is the root class of all classes. The sequence is ordered so that a class always appears before its parents, and if there are multiple parents, they keep the same order shown in the tuple of base classes.

The MRO shown above is the one order that follows from those constraints:

  • LoggingOD precedes its parents, LoggingDict and OrderedDict
  • LoggingDict precedes OrderedDict because LoggingOD.__bases__ is (LoggingDict, OrderedDict)
  • LoggingDict precedes its parent which is dict
  • OrderedDict precedes its parent which is dict
  • dict precedes its parent which is object

The process of solving those constraints is known as linearization. There are a number of good papers on the subject, but we only need to know the two constraints: children precede their parents and the order of appearance in bases in respected.

__________________________________________________________________________________________________________________

Practical Advice

super() is in the business of delegating method calls to some class in the instance’s ancestor tree. For reorderable method calls to work, the classes need to be designed cooperatively. This presents three easily solved practical issues:

  • the method being called by super() needs to exist
  • the caller and callee need to have a matching argument signature
  • and every occurrence of the method needs to use super()

1) Let’s first look at strategies for getting the callers arguments to match the signature of the called method. This is a little more challenging than traditional method calls where the callee is known in advance. With super(), the callee is not known at the time a class is written (because a subclass written later may introduce new classes into the MRO).

One approach is to stick with a fixed signature of positional arguments. This works well with methods like __setitem__ which have a fixed signature of two arguments, a key and a value. This technique is shown in the LoggingDict example where __setitem__ has the same signature in both LoggingDict and dict.

A more flexible approach is to have every method in the ancestor tree cooperatively designed to accept a keyword-arguments dictionary, to pop-off any arguments that it needs, and to forward the remaining arguments using **kwds, eventually leaving the dictionary empty for the final call in the chain.

Each level strips-off the keyword arguments that it needs so that the final empty dict can be sent to a method that expects no arguments at all (for example, object.__init__ expects zero arguments):

class Shape:
    def __init__(self, **kwds):
        self.shapename = kwds.pop('shapename')
        super().__init__(**kwds)        

class ColoredShape(Shape):
    def __init__(self, **kwds):
        self.color = kwds.pop('color')
        super().__init__(**kwds)

cs = ColoredShape(color='red', shapename='circle')

2) Having looked at strategies for getting the caller/callee argument patterns to match, let’s now look at how to make sure the target method exists.

The above example shows the simplest case. We know that object has an __init__ method and that object is always the last class in the MRO chain, so any sequence of calls to super().__init__ is guaranteed to end with a call to object.__init__ method. In other words, we’re guaranteed that the target of the super() call is guaranteed to exist and won’t fail with an AttributeError.

For cases where object doesn’t have the method of interest (a draw() method for example), we need to write our own root class that is guaranteed to be called before object(). The responsibility of the root class is simply to eat the method call without using super():

class Root:
    def draw(self):
        pass            # the delegation chain stops here

class Shape(Root):
    def __init__(self, **kwds):
        self.shapename = kwds.pop('shapename')
        super().__init__(**kwds)
    def draw(self):
        print('Drawing.  Setting shape to:', self.shapename)
        super().draw()

class ColoredShape(Shape):
    def __init__(self, **kwds):
        self.color = kwds.pop('color')
        super().__init__(**kwds)
    def draw(self):
        print('Drawing.  Setting color to:', self.color)
        super().draw()

cs = ColoredShape(color='blue', shapename='square')
cs.draw()

If subclasses want to inject other classes into the MRO, they need to also inherit from Root so that no path for calling draw() can reach object without having been stopped by Root.draw(). This should be clearly documented so that someone writing other cooperating classes will know to subclass from Root. This restriction is not much different than Python’s own requirement that all new exceptions must inherit from BaseException.

3) The techniques shown above assure that super() calls a method that is known to exist and that the signature will be correct; however, we’re still relying on super() being called at each step so that the chain of delegation continues unbroken. This is easy to achieve if we’re designing the classes cooperatively – just add a super() call to every method in the chain.

__________________________________________________________________________________________________________________

How to Incorporate a Non-cooperative Class

Occasionally, a subclass may want to use cooperative multiple inheritance techniques with a third-party class that wasn’t designed for it (for example, a method of interest doesn’t use super() or a class of interest doesn’t inherit from the root class). This situation is easily remedied by creating a wrapper class that plays by the rules.

For example, the following class does not make super() calls and has an __init__() signature that is incompatible with object.__init__:

class Moveable:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def draw(self):
        print('Drawing at position:', self.x, self.y)

If we want to use this class with our cooperatively designed ColoredShape hierarchy, we need to make a wrapper that is responsible for adding the super() calls:

class MoveableWrapper(Root):
    def __init__(self, **kwds):
        x = kwds.pop('x')
        y = kwds.pop('y')
        self.movable = Moveable(x, y)
        super().__init__(**kwds)
    def draw(self):
        self.movable.draw()
        super().draw()

class MovableColoredShape(ColoredShape, MoveableWrapper):
    pass

MovableColoredShape(color='blue', shapename='triangle', x=10, y=20).draw()

__________________________________________________________________________________________________________________

Complete Example – Just for Fun

In Python 2.7 and 3.2, the collections module has both a Counter class and an OrderedDict class. Those are easily composed to make an OrderedCounter:

from collections import Counter, OrderedDict

class OrderedCounter(Counter, OrderedDict):
     'Counter that remembers the order elements are first encountered'

     def __repr__(self):
         return '%s(%r)' % (self.__class__.__name__,
                            OrderedDict(self))

     def __reduce__(self):
         return self.__class__, (OrderedDict(self),)

oc = OrderedCounter('abracadabra')

__________________________________________________________________________________________________________________

Notes and References

  •  The Python 3 version of super() is used in this post. The full working source code can be found at:  Recipe 577720
  • In Python 2, the current class and instance need to be listed explicitly:
    class LoggingDict(dict):
        def __setitem__(self, key, value):
            logging.info('Setting %r to %r' % (key, value))
            return super(LoggingDict, self).__setitem__(key, value)

    The full working source code using Python 2.7 is at Recipe 577721

  • When subclassing a builtin such as dict(), it is often necessary to override or extend multiple methods at a time. In the above example, the extension of __setitem__ isn’t used by other methods such as update(), so it may be necessary to extend those also. This isn’t unique to super(); rather, it comes-up any time builtins are being subclassed.
  • Good write-ups for linearization algorithms can be found at Python MRO documentation and at Wikipedia entry for C3 Linearization

__________________________________________________________________________________________________________________

Acknowledgements

Serveral Pythonistas did a pre-publication review of this article.  Their comments helped improve it quite a bit.

They are:  Laura Creighton, Alex Gaynor, Philip Jenvey, Brian Curtin, David Beazley, Chris Angelico, Jim Baker, Ethan Furman, and Michael Foord.  Thanks one and all.

Explore posts in the same categories: Algorithms, Documentation, Inheritance, Open Source, Python
2 bloggers like this post.

8 Comments on “Python’s super() considered super!”

  1. Matías Says:

    This post rocks!. I was aware of super() but this information opens my eyes to a new world of posibilities. Thank you.

  2. Bob Says:

    This is one of the two annoyances with Python (the other is the GIL).

    C++ just does this *so* much better.

  3. Chris Torek Says:

    The fact that Python 2.x requires the class-name (and of course “self”) as an argument to super() is problematic, wiping out your first “benefit of indirection”. This was always my pet peeve with super(). I know it seems minor, but I always found it tipped the balance between “always use super” and “don’t bother with super if you don’t need it” over towards “don’t bother”.

    Fortunately, “fixed in Python 3″….

    • rhettinger Says:

      No one really liked the Python 2 syntax though it did have the advantage of being explicit about the two inputs to the computation (the mro of self and the current position in the mro).

      The first advantage listed in the post still applies though. It is only the current class that is referenced explicitly. The parent class is still reached through indirection, so you can change the bases and the super() calls will follow automatically.

      Thank you the reply and your insight.

      Raymond

  4. Blue Havana Says:

    For Python 2, instead of using an explicit class, you can use ‘super(type(self), self)’. Makes the code more maintainable if you change the name of your class.


    • Unfortunately that doesn’t work:

      >>> class A(object):
      … def __init__(self):
      … super(type(self), self).__init__()

      >>> class B(A):
      … def __init__(self):
      … super(type(self), self).__init__()

      >>> a = A()
      >>> b = B() # doctest: +IGNORE_EXCEPTION_DETAIL
      Traceback (most recent call last):

      RuntimeError: maximum recursion depth exceeded while calling a
      Python object

      When calling A.__init__ from B.__init__, type(self) is B.

  5. Ivan Savov Says:

    Very good post!
    Thanks for the write up!

    I will need to super() in some code quite soon, and now I feel much comfortable getting to work on my re-factoring…


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>