Python super() Function: Call Parent Class Methods | Syntax, Examples and Use Cases

Introduction: Python super() Function

A child class often needs to reuse methods or constructors from its parent class. This is common when extending existing functionality instead of writing the same code again.

Calling the parent class directly works, but it can make the code harder to maintain. The Python super() Function provides a cleaner way to access parent class methods.

What it is: The super() function is a built-in Python function that returns a temporary object of the parent class. It allows a child class to call methods and constructors defined in its parent class.

See a quick example to understand how the Python super() Function works.

You can also check its real-world use cases to see where this function is commonly used.

Before exploring practical examples and use cases, learn its syntax, parameters, and return value.

💡 Tip: The super() function is mainly used with inheritance. It helps child classes reuse parent class code without referring to the parent class name directly. To explore more built-in functions, visit the Python Built-in Functions Learning Guide.

Syntax, Parameters, Return Value and Examples: Python super() Function

The section below covers the syntax, parameters, return value, and a quick example of the Python super() Function.

Syntax

super()

# or

super(type, object_or_type)

Parameters

Parameter Description
type (optional) The class where the method lookup starts.
object_or_type (optional) An object or class used to determine the method resolution order (MRO).

In modern Python code, super() is usually called without passing any arguments.

Return Value

Return Value Description
super Returns a temporary object that provides access to methods and attributes of the parent class.

Quick Example

The following example calls the parent class constructor from a child class.

class Person:

    def __init__(self):
        print("Person constructor")

class Student(Person):

    def __init__(self):
        super().__init__()

Student()

# Output:
Person constructor

The super() function calls the constructor of the parent class. This allows the child class to reuse the existing constructor instead of writing the same code again.

How the Python super() Function Works

  • The super() function returns a temporary parent class object.
  • It allows a child class to call methods from its parent class.
  • It is commonly used to call the parent class constructor.
  • It follows Python’s Method Resolution Order (MRO).
  • It removes the need to write the parent class name directly.
  • It helps make inheritance easier to maintain.

Examples: Python super() Function

The examples below show different ways to use the Python super() Function.

Example 1: Calling the Parent Class Constructor

class Person:

    def __init__(self):
        print("Person constructor")

class Student(Person):

    def __init__(self):
        super().__init__()
        print("Student constructor")

Student()

# Output:
Person constructor
Student constructor

Explanation: The super() function calls the parent class constructor first. After that, the child class constructor continues its own work.

Example 2: Calling a Parent Class Method

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: The child class first runs the parent class method by using super(). It then adds its own behavior.

Example 3: Reusing Initialization Code

class Employee:

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

class Manager(Employee):

    def __init__(self, name, department):
        super().__init__(name)
        self.department = department

manager = Manager("Emma", "Sales")

print(manager.name)
print(manager.department)

# Output:
Emma
Sales

Explanation: The child class reuses the parent class constructor to initialize the name attribute. Only the new attribute is added in the child class.

Example 4: Using super() in Multi-Level 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: Each class calls the method from its parent. This creates a chain where every class contributes its own output.

Example 5: Calling super() with Class and Object

class Parent:

    def message(self):
        print("Parent method")

class Child(Parent):

    def message(self):
        super(Child, self).message()
        print("Child method")

obj = Child()

obj.message()

# Output:
Parent method
Child method

Explanation: This example uses the older form of super(). It produces the same result but is mainly seen in older Python code.

Example 6: Accessing a Parent Class Method Without Repeating the Class Name

class Vehicle:

    def start(self):
        print("Vehicle started")

class Car(Vehicle):

    def start(self):
        super().start()
        print("Car is ready")

car = Car()

car.start()

# Output:
Vehicle started
Car is ready

Explanation: The child class does not call Vehicle.start() directly. Using super() makes the code easier to maintain if the parent class name changes later.

Example 7: Using super() in Multiple Inheritance

class A:

    def show(self):
        print("Class A")

class B(A):

    def show(self):
        super().show()
        print("Class B")

class C(A):

    def show(self):
        super().show()
        print("Class C")

class D(B, C):

    def show(self):
        super().show()
        print("Class D")

obj = D()

obj.show()

# Output:
Class A
Class C
Class B
Class D

Explanation: The super() function follows Python’s Method Resolution Order (MRO). It decides which parent method should run next without requiring you to manage the order yourself.

Use Cases: When to use the super() Function

The Python super() Function is useful in situations like these:

  • Calling a parent class constructor.
  • Reusing methods from a parent class.
  • Reducing duplicate code in child classes.
  • Working with multi-level inheritance.
  • Supporting multiple inheritance using Method Resolution Order (MRO).
  • Making inheritance easier to maintain.

Key Takeaways: super() Function

Here are the main points to remember about the Python super() Function:

  • The super() function returns a temporary parent class object.
  • It is mainly used in inheritance.
  • It allows child classes to call parent class methods and constructors.
  • It follows Python’s Method Resolution Order (MRO).
  • It removes the need to write the parent class name directly.
  • It helps create cleaner and easier-to-maintain object-oriented code.

The super() function makes it easier for child classes to reuse parent class functionality. It reduces duplicate code and helps keep inheritance simple and organized.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top