The super() function in Python is used to access methods and attributes of a parent or superclass from a child class. It is mainly used with inheritance when a child class needs to reuse or extend the functionality of its parent class. The super() function makes inheritance easier to manage by avoiding the need to directly refer to the parent class name. It is commonly used with overridden methods and constructors, allowing child classes to reuse parent functionality while adding their own behavior.
Table of Contents
What is super() Function in Python?
The super() function returns a proxy object that allows a child class to access methods and attributes from a parent class or the next class in the method resolution order (MRO).
It is most commonly used when a child class overrides a method but still needs to execute the parent class's implementation.
Example:
# Python program to understand the super() function
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
super().sound()
print("Dog barks")
dog = Dog()
dog.sound()
Output:
Animal makes a sound
Dog barks
Explanation:
- Animal is the parent class and contains the sound() method.
- Dog inherits from Animal and overrides the sound() method.
- super().sound() calls the parent class implementation.
- The Dog class then executes its own statement.
- This allows the child class to extend the parent's behavior.
Why Use super() Function in Python?
The super() function is useful when working with inheritance and helps child classes reuse functionality from parent classes.
- Reuses parent functionality: A child class can use an existing method from its parent class.
- Avoids directly naming the parent class: super() can access the next class in the method resolution order without explicitly writing the parent class name.
- Supports method overriding: It allows a child class to extend rather than completely replace a parent method.
- Useful with constructors: It allows a child class to call the parent's __init__() method.
- Works with multiple inheritance: super() follows Python's method resolution order, making cooperative multiple inheritance easier to implement.
 function_1789022181.png)
How super() Function Works in Python?
The super() function is commonly used inside a child class to call functionality defined higher in the inheritance hierarchy.
Syntax:
super().method_name()
The super() function can be used to call a parent or next-in-line implementation without explicitly specifying the parent class.
Example:
# Python program to demonstrate how super() works
class Parent:
def show(self):
print("Parent class method")
class Child(Parent):
def show(self):
super().show()
print("Child class method")
child = Child()
child.show()
Output:
Parent class method
Child class method
Explanation:
- Parent contains the show() method.
- Child inherits from Parent.
- Child overrides the show() method.
- super().show() calls the inherited implementation.
- The child class then adds its own behavior.
super() with Method Overriding
The super() function is frequently used with method overriding. When a child class overrides a method, super() allows it to call the original implementation before or after adding new functionality.
Example:
# Python program to demonstrate super() with method overriding
class Vehicle:
def start(self):
print("Vehicle is starting")
class Car(Vehicle):
def start(self):
super().start()
print("Car starts with a key")
car = Car()
car.start()
Output:
Vehicle is starting
Car starts with a key
Explanation:
- Vehicle defines the start() method.
- Car overrides the start() method.
- super().start() calls the parent implementation.
- The child class executes its own statement.
- Both parent and child behavior are performed.
super() with Constructors
The super() function is commonly used inside a child class constructor to call the parent class's __init__() method. This allows the child class to reuse the initialization logic defined in the parent class.
Example:
# Python program to demonstrate super() with constructors
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, name, course):
super().__init__(name)
self.course = course
student = Student("Tanavi", "BSc IT")
print(student.name)
print(student.course)
Output:
Tanavi
BSc IT
Explanation:
- Person defines the name attribute inside its constructor.
- Student inherits from Person.
- The Student constructor receives name and course.
- super().__init__(name) calls the parent constructor.
- The course attribute is initialized inside the child class.
super() with Attributes
The super() function can also be used to access attributes through a parent-class property or other descriptor. However, ordinary instance attributes are usually accessed through self, while super() is most commonly useful for inherited methods and properties.
Example:
# Python program to demonstrate super() with a property
class Person:
@property
def role(self):
return "Person"
class Student(Person):
@property
def role(self):
return super().role + " - Student"
student = Student()
print(student.role)
Output:
Person - Student
Explanation:
- Person defines a role property.
- Student overrides the role property.
- super().role accesses the parent property's value.
- The child property adds " - Student".
- The final value is returned when student.role is accessed.
super() with Multiple Inheritance
Python supports multiple inheritance, where a class can inherit from more than one parent class. In such cases, super() follows the method resolution order (MRO) to determine which implementation should be called next.
Example:
# Python program to demonstrate super() with
# multiple inheritance
class A:
def show(self):
print("Class A")
class B(A):
def show(self):
super().show()
print("Class B")
class C(B):
def show(self):
super().show()
print("Class C")
obj = C()
obj.show()
Output:
Class A
Class B
Class C
Explanation:
- A defines the show() method.
- B inherits from A and overrides show().
- C inherits from B and also overrides show().
- Each super().show() moves to the next implementation according to the MRO.
- The methods are executed in the order A, B, and C.
Note:
In multiple inheritance, super() does not simply mean "call my direct parent." It follows the class's method resolution order.
super() Without Arguments
Inside a method of a class, Python allows the zero-argument form super() to refer to the appropriate next class in the method resolution order.
Example:
# Python program to demonstrate super() without arguments
class Parent:
def display(self):
print("Parent display method")
class Child(Parent):
def display(self):
super().display()
print("Child display method")
child = Child()
child.display()
Output:
Parent display method
Child display method
Explanation:
- Child inherits from Parent.
- Child overrides the display() method.
- super() automatically uses the current class and instance context.
- super().display() finds the next implementation in the MRO.
- The parent method is executed before the child method.
super() with Arguments
The two-argument form of super() can explicitly specify the current class and object.
Syntax:
super(CurrentClass, object)
Example:
# Python program to demonstrate super() with arguments
class Parent:
def show(self):
print("Parent class")
class Child(Parent):
def show(self):
super(Child, self).show()
print("Child class")
child = Child()
child.show()
Output:
Parent class
Child class
Explanation:
- Child inherits from Parent.
- Child overrides the show() method.
- super(Child, self) creates a proxy that searches after Child in the MRO.
- .show() calls the parent implementation.
- The child implementation then continues.
Note:
In modern Python code, super() without arguments is usually preferred because it is shorter and less error-prone.
Advantages of super() Function
The super() function provides several advantages when working with inheritance.
- Reduces code duplication: Parent functionality can be reused instead of rewriting the same code.
- Makes inheritance cleaner: Child classes can extend parent behavior without directly referring to the parent class name.
- Supports method overriding: It allows the child class to reuse the overridden method's implementation.
- Useful for constructors: Parent initialization can be reused from child constructors.
- Supports multiple inheritance: super() works with Python's method resolution order and supports cooperative inheritance patterns.
Common Beginner Mistakes
1. Forgetting super() in a Child Constructor: If the parent class initializes important attributes, the child constructor should call the parent constructor when that initialization is required.
Wrong:
# Python program showing a missing parent constructor call
class Person:
def __init__(self, name):
self.name = nameclass Student(Person):
def __init__(self, name, course):
self.course = coursestudent = Student("Tanavi", "BSc IT")
Correct:
# Python program showing the correct use of super() in a constructor
class Person:
def __init__(self, name):
self.name = nameclass Student(Person):
def __init__(self, name, course):
super().__init__(name)
self.course = coursestudent = Student("Tanavi", "BSc IT")
print(student.name)
print(student.course)
Output:
Tanavi
BSc IT
2. Calling the Parent Class Directly When super() Is More Appropriate: Directly calling Parent.method(self) can make inheritance hierarchies harder to maintain, especially with multiple inheritance.
Example:
# Python program showing the preferred use of super()
class Parent:
def show(self):
print("Parent method")class Child(Parent):
def show(self):
super().show()
print("Child method")child = Child()
child.show()
Output:
Parent method
Child method
3. Calling super() Outside the Appropriate Class Context: The zero-argument form of super() is intended for use within a method where Python can determine the relevant class and instance context.
Wrong:
# Python program showing an invalid use of zero-argument super()
class Parent:
passprint(super())
Correct:
# Python program showing the correct use of super()
class Parent:
def show(self):
print("Parent method")class Child(Parent):
def show(self):
super().show()child = Child()
child.show()
Output:
Parent method
4. Assuming super() Always Means the Direct Parent: In multiple inheritance, super() follows the method resolution order rather than simply calling a direct parent class.
Example:
# Python program to demonstrate super() and method resolution order
class A:
def show(self):
print("A")
class B(A):
def show(self):
super().show()
print("B")
class C(B):
def show(self):
super().show()
print("C")
obj = C()
obj.show()
Output:
A
B
C
5. Using the Wrong Class or Object with Two-Argument super(): When using super(CurrentClass, object), the class and object must match the inheritance context correctly.
Wrong:
# Python program showing an incorrect two-argument super() call
class Parent:
def show(self):
print("Parent")class Child(Parent):
def show(self):
super(Parent, self).show()child = Child()
child.show()
Correct:
# Python program showing a correct two-argument super() call
class Parent:
def show(self):
print("Parent")class Child(Parent):
def show(self):
super(Child, self).show()child = Child()
child.show()
Output:
Parent
Conclusion
The super() function in Python is an important feature of object-oriented programming that allows child classes to access functionality from the next class in the method resolution order. It is commonly used with method overriding and constructors to reuse or extend parent-class behavior.
Using super() helps reduce code duplication and makes inheritance hierarchies easier to maintain. It is especially important when working with multiple inheritance because it follows Python's method resolution order. Understanding super() makes it easier to build flexible and reusable Python applications.
Frequently Asked Questions (FAQs)
1. What is the super() function in Python?
The super() function is used to access methods and attributes from a parent class or the next class in the method resolution order.
2. Why is super() used in Python?
super() is used to reuse or extend functionality from a parent class, especially when working with inheritance, method overriding, and constructors.
3. Can super() be used with constructors?
Yes. super() is commonly used to call the parent class's __init__() method from a child class constructor.
4. What is the difference between super() and self?
self refers to the current object, while super() provides access to the next class in the method resolution order for the current class and object.
5. Does super() always call the parent class?
Not necessarily. In simple single inheritance, it commonly reaches the parent implementation, but in multiple inheritance, super() follows the method resolution order.
0 Comments