Python 3 reference · Built-in Functions
Python super() Function
Return a proxy that calls methods of the parent class, so a subclass can extend a method instead of replacing it.
Python 3 Since 3.0
Syntax
super()
super(type, object_or_type)
Parameters
| Parameter | Description |
|---|---|
type, object_or_type optional |
Rarely needed: the explicit two-argument form. |
Return value
A proxy object that forwards method calls to the next class in the MRO.
Examples
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # run the parent's __init__
self.breed = breed
d = Dog("Rex", "collie")
print(d.name, d.breed)
Output
Rex collie
class Logger:
def log(self, msg):
print("LOG:", msg)
class TimestampLogger(Logger):
def log(self, msg):
super().log(f"[12:00] {msg}")
TimestampLogger().log("started")
Output
LOG: [12:00] started
Good to know
With multiple inheritance, super() follows the method resolution order (Class.__mro__), not just the
direct parent, so every class in the chain should call super() for cooperative methods.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
issubclass()· Return True if a class is a subclass of another class (or of any class in a tuple or union). A class is a subclass of itself.isinstance()· Return True if an object is an instance of a class, one of its subclasses, or any class in a tuple or union of classes.object()· Return a new featureless object. object is the base class of every Python class and provides the default methods.classmethod()· Turn a method into a class method that receives the class (cls) instead of an instance. Usually written as @classmethod.- Python Inheritance · tutorial