super() in Python With Examples

In this tutorial we’ll see what is super() function in Python and how to use it.

Python super() function

super() is a built-in function in Python which returns a proxy object that can be used to delegate method calls (including call to constructor) to immediate parent class from the child class.

Syntax of super()

Syntax of super() in Python is as following-

super([type[, object-or-type ]])

Suppose you have a child class C inheriting from base class B defined as given below-

class C(B):
	def method(self, arg):
		super().method(arg)

In the method there is a call to method which is defined in the super class using the super() function. Since both of the parameters are optional so this call super().method(arg) is equivalent to super(C, self).method(arg) where both of the parameters are passed.

Use of super()

1. Since super() is a proxy object (indirect reference) to the super class so rather than using the super class name directly you can use super() to call super class method. If we take the following class structure

class C(B):
	def method(self, arg):
		super().method(arg)

Here you could have also called method of the parent class by using this statement-

B.method(arg)

Using super() sub-class is not tightly bound to the same super class. In case you later want to change the class class C inherits from, super() will point to the new super class.

class C(A):
	def method(self, arg):
		super().method(arg)

Now super().method(arg) calls the method of the class A.

2. Usage of super() also helps in reducing the code redundancy. See the example to know more.

Python example using super()

In the example there is a class called Person that acts as a base class and another class Employee that inherits from Person class. Employee class also adds properties of its own and also overrides the parent class method.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display_info(self):
        print('In display_info method of Person class')
        print('Name:', self.name)
        print('Age:', self.age)


class Employee(Person):
    def __init__(self, person_id, department, name, age):
        self.name = name
        self.age = age
        self.person_id = person_id
        self.department = department

    def display_info(self):
        print('In display_info method of Employee class')
        print('Id:', self.person_id)
        print('Name:', self.name)
        print('Age:', self.age)
        print('Department:', self.department)

In the child class Employee you can notice the redundancy of initializing the parent class’ fields in the constructor though there is a constructor in the parent class which is already doing that. Same way in the display_info () method we have print statement to print name and age too though there is a method in Person class which is already doing that.

Here we can use super() to call constructor of the parent class to initialize fields that are there in the parent class. Using super() we can also call the parent() class method. Here is the modified Employee class-

class Employee(Person):
    def __init__(self, person_id, department, name, age):
        super().__init__(name, age)
        self.person_id = person_id
        self.department = department

    def display_info(self):
        super().display_info()
        print('In display_info method of Employee class')
        print('Id:', self.person_id)
        print('Department:', self.department)

As you can see super() is used to call the parent class constructor with the fields that can be initialized there.

super().__init__(name, age)

From the overridden display_info() method there is a call to the method of the parent class.

super().display_info()

On creating an object of Employee class and calling the method using that object

e = Employee(1, "IT", "Michael Weyman", 42)
e.display_info()

Output

In display_info method of Person class
Name: Michael Weyman
Age: 42
In display_info method of Employee class
Id: 1
Department: IT

Related Posts

That’s all for the topic super() in Python With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.