logo

Using super()

When overriding a parent method, you often want to extend it rather than replace it completely. super() calls the parent's version.

class Animal:
    def __init__(self, name):
        self.name = name

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)  # Call parent's __init__
        self.breed = breed

Without super(), you'd have to duplicate the parent's initialization code.

buddy = Dog("Buddy", "Golden Retriever")
print(buddy.name)   # Buddy
print(buddy.breed)  # Golden Retriever

super() keeps your code DRY (Don't Repeat Yourself) and ensures parent initialization runs properly.

For super() patterns, see my Python OOP course.