super
Access the methods of the parent class from a method of a child class.
Python 3.13
super()
super(type[, object_or_type])Parameters
- typetypeoptional
- The class after which the method lookup starts; it can be omitted inside a method defined in a class.
- object_or_typeAnyoptional
- The object or class the method is called for; inside a method it is filled in automatically.
Return value
A proxy object through which the methods of the parent class are called.
Examples
Calling the parent __init__
Python 3.13
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
dog = Dog('Rex', 'beagle')
print(dog.name, dog.breed)Rex beagle
Extending a parent method
Python 3.13
class Greeter:
def greet(self):
return 'Hello'
class LoudGreeter(Greeter):
def greet(self):
return super().greet().upper() + '!'
print(LoudGreeter().greet())HELLO!
Details
Without arguments, super() works only inside a method defined in a class: Python fills in the current class and self. If a child __init__ does not call super().__init__(...), the attributes set by the parent do not appear. With multiple inheritance, super() follows the method resolution order (MRO), not necessarily the direct parent.
