super()
Gives access to the parent class's methods from inside a child class, most often to run the parent's __init__ before adding your own setup.
super()
super(type, object_or_type)When a class inherits from another, it can replace (override) the parent's methods. super() lets the child still use the parent's version: super().__init__(name) runs the parent's setup, super().describe() gets the parent's text so you can add to it.
The most common use is in __init__. If the child defines its own __init__, the parent's one no longer runs automatically. Call super().__init__(...) so the parent can set its attributes too.
Inside a class, write super() with empty brackets: Python works out the class and the object for you. With multiple inheritance, super() does not always mean "my direct parent": it follows the class's method resolution order (MRO), so every class in the family runs exactly once.
Parameters
| Parameter | What it is |
|---|---|
typeoptional | The class to start searching after. Leave it out inside a method. |
object_or_typeoptional | The object (or class) the method is called on. Leave it out inside a method. |
Return value
A proxy object: calling a method on it runs the next class's version of that method.
Examples
1Run the parent's __init__
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("Luna", "beagle")
print(d.name, "is a", d.breed)Luna is a beagle
2Extend a method instead of replacing it
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def describe(self):
return f"{self.name}, salary {self.salary}"
class Manager(Employee):
def __init__(self, name, salary, team):
super().__init__(name, salary)
self.team = team
def describe(self):
return super().describe() + f", leads {len(self.team)} people"
m = Manager("Sara", 5200, ["Ali", "Tom", "Eva"])
print(m.describe())Sara, salary 5200, leads 3 people
3Add behaviour to a built-in type
Override append() to print a message, then let list do the real work through super().
class LoggedList(list):
def append(self, item):
print("Adding", item)
super().append(item)
basket = LoggedList()
basket.append("apples")
basket.append("bread")
print(basket)Adding apples Adding bread ['apples', 'bread']
4super() with multiple inheritance
class Base:
def setup(self):
print("Base setup")
class Logging(Base):
def setup(self):
print("Logging setup")
super().setup()
class Saving(Base):
def setup(self):
print("Saving setup")
super().setup()
class App(Logging, Saving):
def setup(self):
print("App setup")
super().setup()
App().setup()
print([c.__name__ for c in App.__mro__])App setup Logging setup Saving setup Base setup ['App', 'Logging', 'Saving', 'Base', 'object']
In Logging, super() leads to Saving, not straight to Base: it follows the order shown on the last line, so Base runs only once.
Common mistakes
Forgetting super().__init__()
If the child's __init__ does not call the parent's, the attributes the parent sets never exist.
class Vehicle:
def __init__(self, wheels):
self.wheels = wheels
class Bike(Vehicle):
def __init__(self, color):
self.color = color
b = Bike("red")
try:
print(b.wheels)
except AttributeError:
print("AttributeError: Bike has no wheels")AttributeError: Bike has no wheels
Passing self to super().method()
super().__init__(self, name) passes self twice: super() already binds the object. Write super().__init__(name). The old form Parent.__init__(self, name) needs self, but it skips the MRO and can run a class twice in multiple inheritance.
Questions people ask
What does super().__init__() do?
It runs the __init__ method of the next class up (usually the parent), so the parent can set up its part of the object.
What is the difference between super() and super(Child, self)?
None inside a method. super(Child, self) is the older Python 2 spelling; in Python 3 the empty super() does the same and is less error-prone.
Can I use super() outside a class?
The empty form only works inside a method of a class. Elsewhere you must pass both arguments, which is rare.