all Python 3OOPfunction
super()
inheritance
Returns a proxy object for the parent class. Used to call the parent's methods without naming it explicitly.
Syntax
super().__init__(...)
super().method(...)
super(type, obj_or_type)
super() doesn't call "the parent" literally — it forwards to the next class in the MRO (method resolution order), which under multiple inheritance may not be the base you wrote in the class header. Forget to call super().__init__() and the parent's initialization silently never runs, leaving some attributes unset.
Examples
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
d = Dog('Rex', 'Lab')
print(d.name, d.breed) # → Rex Lab
class Base:
def greet(self):
return 'Hello from Base'
class Child(Base):
def greet(self):
return super().greet() + ' and Child'
print(Child().greet()) # → Hello from Base and Child
class LoggedList(list):
def append(self, item):
print(f'Adding {item}')
super().append(item)
ll = LoggedList()
ll.append(1) # → Adding 1
print(ll) # → [1]
# Множественное наследование: super() идёт по MRO, а не к «родителю»
class A:
def __init__(self):
print('A')
class B(A):
def __init__(self):
super().__init__()
print('B')
class C(A):
def __init__(self):
super().__init__()
print('C')
class D(B, C):
def __init__(self):
super().__init__()
print('D')
D() # → A / C / B / D
print([k.__name__ for k in D.__mro__]) # → ['D', 'B', 'C', 'A', 'object']
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy