Python super() Method

The super() method returns a proxy object that allows us to access methods of the base class.

Syntax:

super(type, object)

Parameters:

  1. type: (Optional) The class name whose base class methods needs to be accessed
  2. object: (Optional) An object of the class or self.

Return type:

No return value.

The following example demonstrates how the derived class can call base class using the super() method.

Example:
class person:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname
    def fullname(self):
        print(firstname, ' ', lastname)
    
class student(person):
    def __init__(self, firstname, lastname, grade):
        self.grade = grade
        super().__init__(firstname, lastname) # calling base constructor
    def display_details():
        super().fullname() # calling base class method
        print('Grade ', self.grade)
        
std = student('James', 'Bond', '10')
std.display_details()
Output
James Bond<br>
Grade 10

In the above example, super().__init__(firstname, lastname) in the init method of the student class call's the base class person's init method and pass parameters. In the same way, super().fullname() calls person.fullname() method. The super().fullname() can be called as super(student, self).fullname().

Visit inheritance in Python for more information.

Want to check how much you know Python?