Python Tutorial Python Advanced Python References Python Libraries

Python super() Function



The Python super() function is used to initialize base class object in derived class (in single inheritance), hence helps derived class to inherit all the methods and properties of base class without explicitly mentioning base class name.

Syntax

super()

Parameters

No parameter is required.

Example:

In the example below, a base class called polygon and derived class called square are created. According to the inheritance property, the __init__() function of base class polygon is overridden by __init__() function of derived class square. Hence, object of derived class square requires only one parameter to initialize the object. To access all the methods and properties of base class polygon, super() function is used in derived class square.

class polygon:
  def __init__(self, length, breadth):
    self.length = length
    self.breadth = breadth
  def area(self):
    return self.length * self.breadth
  
class square(polygon):
   def __init__(self, side):
      super().__init__(side, side)
    
MySquare = square(3)
print(MySquare.area())

The output of the above code will be:

9

❮ Python Built-in Functions