Python super() Function
The super() function in Python is used to allow access to methods and properties of base or parent class. For example:
class Base: def __init__(self, x, y): self.valOne = x self.valTwo = y def sum(self): res = self.valOne + self.valTwo return res class Child(Base): def __init__(self, a, b): super().__init__(a, b) ob = Child(50, 60) print(ob.sum())
The output is:
Python super() Function Syntax
The syntax of super() function in Python, is:
followed by __init__() function with arguments to pass.
Note: The super() is useful to access inherited methods that have been overridden in a class. Also helps to work with inheritance.
Python super() Function Example
Here is an example of super() function in Python. I've created two classes namely Base and Child, inside the Child class, I've used super() function, so that I can use methods and properties of Base class using the object of Child class, of course.
class Base: def __init__(self, x): print("In 'Base' class and '__init__' Function") self.val = x def myFunOne(self): print("In 'Base' class and 'myFunOne' Function") print("The value is:", self.val) def myFunTwo(self): print("In 'Base' class and 'myFunOne' Function") print("The square of", self.val, "is:", self.val*self.val) class Child(Base): def __init__(self, a): print("In 'Child' class and '__init__' Function") super().__init__(50)
Now using the following statement:
we will get the following output:
In 'Child' class and '__init__' Function In 'Base' class and '__init__' Function
That is, after creating an object of Child class, both the __init__() method automatically gets called. But first the __init__() method of Child class is called.
Now the following code/statement:
produces:
In 'Base' class and 'myFunOne' Function The value is: 50
Similarly the following statement:
produces:
In 'Base' class and 'myFunOne' Function The square of 50 is: 2500
Let's create another program, demonstrating the super() function in Python:
class Base: def __init__(self, x): print("Inside 'Base' class and '__init__' Function") self.val = x def myFunOne(self): print("Inside 'Base' class and 'myFunOne' Function") print("The value is", self.val) class ChildOne(Base): def __init__(self, a): print("Inside 'ChildOne' class and '__init__' Function") self.val = a super().__init__(a) def myFunTwo(self): print("Inside 'ChildOne' class and 'myFunTwo' Function") sq = self.val * self.val print("The square of", self.val, "is", sq) class ChildTwo(ChildOne): def __init__(self, a): print("Inside 'ChildTwo' class and '__init__' Function") self.val = a super().__init__(a) def myFunThree(self): print("Inside 'ChildTwo' class and 'myFunThree' Function") cu = self.val*self.val*self.val print("The cube of", self.val, "is", cu) ob = ChildTwo(50) print("---------------------------------") ob.myFunOne() print("---------------------------------") ob.myFunTwo() print("---------------------------------") ob.myFunThree()
The snapshot given below shows the sample output produced by above Python program:
Note: The super() function is used to, of course avoid referring the base class explicitly.
« Previous Function Next Function »
Liked this post? Share it!