super()
super()
Section titled โsuper()โWhat it is
Section titled โWhat it isโsuper() returns a proxy object that lets a subclass call a method from the next class in its method resolution order (MRO) โ most commonly used to call a parentโs __init__ so the subclass extends rather than completely overrides it.
Before this feature
Section titled โBefore this featureโCalling the parent class by name directly works for simple single inheritance, but hardcodes the parentโs name and breaks down under multiple inheritance:
class Animal: def __init__(self, name): self.name = name
class Dog(Animal): def __init__(self, name, breed): Animal.__init__(self, name) # hardcodes the parent class name self.breed = breedAfter this feature
Section titled โAfter this featureโclass Animal: def __init__(self, name): self.name = name
class Dog(Animal): def __init__(self, name, breed): super().__init__(name) # no hardcoded parent name self.breed = breed
d = Dog("Rex", "Labrador")print(d.name, d.breed) # Rex Labrador# super() also works for calling any method, not just __init__class Shape: def describe(self): return "A shape"
class Circle(Shape): def describe(self): return super().describe() + ", specifically a circle"
print(Circle().describe()) # "A shape, specifically a circle"Why this is better
Section titled โWhy this is betterโ- No hardcoded parent name:
super().__init__(name)keeps working even if the class hierarchy is later restructured (a new intermediate class inserted, for example). - Correct under multiple inheritance:
super()follows the actual MRO, not just โmy literal parentโ โ essential for cooperative multiple inheritance where each class in a chain should run exactly once (see the MRO page for the full mixin example). - Extends instead of replaces: calling
super().__init__(...)then adding subclass-specific setup is the standard pattern for โdo what the parent does, plus a bit more.โ
Key notes / edge cases
Section titled โKey notes / edge casesโ- In Python 3,
super()with no arguments works inside an instance method (it infers the class and instance automatically); the oldsuper(Dog, self)form still works but is rarely needed. - Forgetting to call
super().__init__(...)in a subclassโs__init__means the parentโs setup (e.g.self.name = name) never runs โ a common bug where an inherited attribute is simply missing. super()resolves relative to the MRO of the actual instanceโs class, not the class where thesuper()call is written โ this is what makes it correct even when called from deep in a multi-level hierarchy.
Quick practice
Section titled โQuick practiceโ-
Whatโs the main advantage of
super().__init__(name)overAnimal.__init__(self, name)?Answer
It doesn't hardcode the parent class's name, and it follows the MRO correctly under multiple inheritance โ `Animal.__init__(self, ...)` always calls exactly that class, which can be wrong or cause a class to run twice in a diamond hierarchy. -
What happens if a subclassโs
__init__never callssuper().__init__(...)?Answer
The parent class's initialization never runs, so any attributes it would have set are simply missing on the instance. -
Does
super()require explicit arguments in Python 3?Answer
No โ `super()` with no arguments works inside instance methods; it automatically infers the class and instance from context.