Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Built-in Functions

Python super() function

Reference for Python super(): call parent-class methods, cooperative multiple inheritance and the MRO, zero-argument form, __init__ chaining and common errors.

The built-in super() returns a proxy object that lets you call methods of a parent (or sibling) class from inside a subclass. Its most common use is super().__init__(...) to run the parent's initializer, and super().method() to extend rather than replace inherited behavior.

More precisely, super() does not simply mean "the parent": it looks up the next class in the method resolution order (MRO) of the instance's class. That is what makes cooperative multiple inheritance work.

Python
class Child(Parent):
    def __init__(self, name):
        super().__init__(name)

Try it

Python Extending a parent

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Cooperative multiple inheritance

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Syntax

Python
super()                          # inside a method; same as super(__class__, self)
super(type, object_or_type)      # explicit two-argument form
Name Description
type Start the lookup in the class after this one in the MRO.
object_or_type An instance (for instance methods) or a class (for class methods) whose MRO is used.

Return value: a proxy object; attribute access on it finds the next implementation in the MRO and binds it to the instance or class.

Raises: RuntimeError if called without arguments outside a method of a class; TypeError if the second argument is not an instance or subclass of the first; AttributeError if no parent defines the attribute.

Usage notes

  • Zero-argument form only works inside functions defined in a class body. It relies on the hidden __class__ cell and the first argument of the method, so avoid renaming self.
  • Not necessarily the direct parent. In diamond hierarchies super() in Left can reach Right. Design cooperative methods so each one calls super() and accepts extra **kwargs.
  • Forgetting to call it. If a subclass defines __init__ and does not call super().__init__(), the parent's attributes are never set.
  • Special methods are looked up on the type, so super()[0] or super() + x does not work; call super().__getitem__(0) instead.
  • Class methods and static contexts. In a class method, super() binds to the class, which makes alternative constructors inheritable.
  • Mixins usually put super().method() calls so they compose in any order.
  • Not for classes with __slots__ conflicts or when you want a specific parent: name the parent directly, Parent.method(self), though this bypasses the MRO.
  • In Python 2 you had to write super(Child, self); the short form is Python 3 only.