- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
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:
110
Python super() Function Syntax
The syntax of super() function in Python, is:
super()
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:
bo = Child(50)
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:
bo.myFunOne()
produces:
In 'Base' class and 'myFunOne' Function The value is: 50
Similarly the following statement:
bo.myFunTwo()
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 »