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.

Easy Python 3.9+ Aug 9, 2026 OOP & classes 13 views 0 copies

Python code

21 lines
Python 3.9+
class 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

stdout
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

  1. Use `super().__init__(name, species, fur_color)` in Mammal to avoid hardcoding attribute values as in the example
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from OOP & classes

Related tutorials and quizzes for this topic.