The super() Function
Call the parent's methods from the child -- essential for extending behavior.
Introduction
super() lets you call the parent's methods from inside the child class. This is essential when you want to extend rather than completely replace the parent's behavior. You have already seen it briefly -- now let's understand it deeply.
What is super()?
super() is a built-in Python function that returns a temporary object of the parent class. Through this object, you can call any of the parent's methods -- including __init__.
Key idea: super() is your bridge to the parent class. It says: "Run the parent's version of this method."
Using super() in __init__
The most common use of super() is in the child's constructor. Let the parent set up its attributes first, then add the child's own.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age) # call parent's __init__
self.student_id = student_id
s = Student("Alice", 20, "S001")
print(s.name) # Alice -- set by parent
print(s.student_id) # S001 -- set by child
Using super() in Methods
You can also use super() inside regular methods to extend the parent's behavior.
class Animal:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def describe(self):
return f"I am {self.name}"
class Dog(Animal):
def describe(self):
base = super().describe()
return f"{base} and I am a {self.breed}"
d = Dog("Buddy", "Labrador")
print(d.describe())
# I am Buddy and I am a Labrador
Without super() (The Problem)
Without super(), you would have to duplicate the parent's code in the child. This defeats the purpose of inheritance.
# BAD -- duplicating parent code
class Student(Person):
def __init__(self, name, age, student_id):
self.name = name # duplicated!
self.age = age # duplicated!
self.student_id = student_id
# GOOD -- using super()
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age) # reuse parent
self.student_id = student_id
When to Use super()
- Always in __init__ when the parent has one -- ensures parent attributes are set up.
- In methods when you want to extend the parent's behavior, not fully replace it.
- Not needed when you want to completely replace the parent's method with new logic.
Real-World Connection
In an employee system, all employees share a base setup: name, ID, salary. A Manager adds a department and team list on top. A Developer adds a programming language. Using super(), each role reuses the shared setup and only adds what is unique to it.
Best Practices
- Always call
super().__init__()at the beginning of the child's__init__. - Pass the correct arguments that the parent's method expects.
- Use
super()instead of hardcoding the parent class name -- it is cleaner and works with multiple inheritance.
Common Mistakes
- Forgetting to call
super().__init__()-- the parent's attributes will be missing. - Passing
selftosuper().__init__()-- Python handlesselfautomatically withsuper(). - Calling
super()with wrong arguments that do not match what the parent expects.
Practice Exercises
- Create a
Productclass withnameandprice. Create anElectronicschild that addswarranty_yearsusingsuper(). - Create a
Birdclass with adescribe()method. Create aParrotchild that usessuper().describe()and adds "and I can talk!". - Create a
Accountclass with adisplay()method. Create aSavingsAccountthat extendsdisplay()usingsuper()to also show the interest rate.
Summary
super()gives access to the parent class from inside the child.- Use
super().__init__()to initialize parent attributes without duplicating code. - Use
super().method()in regular methods to extend parent behavior. - Without
super(), you would have to copy-paste parent logic into every child.