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.
class Child(Parent): def __init__(self, name): super().__init__(name)
Try it
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.
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
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 renamingself. - Not necessarily the direct parent. In diamond hierarchies
super()inLeftcan reachRight. Design cooperative methods so each one callssuper()and accepts extra**kwargs. - Forgetting to call it. If a subclass defines
__init__and does not callsuper().__init__(), the parent's attributes are never set. - Special methods are looked up on the type, so
super()[0]orsuper() + xdoes not work; callsuper().__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.
Related
object(),isinstance(),issubclass(),classmethod(),type()- Lessons: Inheritance, Classes, OOP principles