How to Call a Parent Class __init__ with super() in Python
Shows how to chain __init__ calls through a class hierarchy using super(), so each class sets its own attributes while reusing the parent's initialization logic.
Python code
21 linesclass Animal:
def __init__(self, name, species):
self.name = name
self.species = species
print(f"Animal init: {self.name}, {self.species}")
class Mammal(Animal):
def __init__(self, name, species, fur_color):
super().__init__(name, species)
self.fur_color = fur_color
print(f"Mammal init: fur={self.fur_color}")
class Dog(Mammal):
def __init__(self, name, breed):
super().__init__(name, "Dog", "brown")
self.breed = breed
print(f"Dog init: breed={self.breed}")
if __name__ == "__main__":
dog = Dog("Rex", "Golden Retriever")
print(f"{dog.name} is a {dog.breed}, species={dog.species}, fur={dog.fur_color}")
Output
Animal init: Rex, Dog
Mammal init: fur=brown
Dog init: breed=Golden Retriever
Rex is a Golden Retriever, species=Dog, fur=brown
How it works
super().__init__(...) in the subclass finds the next class in the method resolution order (MRO) and calls its __init__, letting the parent initialize its own attributes before the child adds more. This keeps initialization DRY and avoids duplicating assignment logic across each subclass. The chain works even with multiple inheritance because super() respects the cooperative MRO. Each __init__ prints a message so you can see the order of initialization from the top of the hierarchy down to the most derived class.
Common mistakes
- Forgetting to call super().__init__() entirely, leaving parent attributes unset
- Calling the parent class directly (e.g., Animal.__init__(self)) instead of super(), which breaks cooperative multiple inheritance
- Passing the wrong number or order of arguments to super().__init__ compared to the parent's signature
Variations
- Use `super().__init__(name, species, fur_color)` in Mammal to avoid hardcoding attribute values as in the example
- Use dataclasses with inheritance to auto-generate __init__ calls across classes
Real-world use cases
- Extending a base model class in an ORM (e.g., Django's models.Model) to add custom fields while keeping the parent's setup.
- Building UI widget hierarchies where each subclass initializes its own layout options after the base widget setup.
- Creating game entity classes (Player, Enemy) that share common attributes like position and health from a base Entity class.
Sponsored
More from OOP & classes
- Add property getter setter validation in Python easy
- Binary Tree Inorder Traversal in Python easy
- Borg pattern shared state in Python medium
- Bridge Pattern in Python: Separate Abstraction from Implementation medium
- Composable Predicates with the &, |, ~ Operators in Python medium
- Composition over Inheritance: How to Build a Wallet Account in Python easy
Keep learning
Related tutorials and quizzes for this topic.