Python super() Function
Returns an object that represents the parent instance which you can use to access functions in the parent class.
Syntax
Python
Copy Code
super()
Example
Python
Copy Code
class Parent: def printMessage(self): print('Messge from parent.') class Child(Parent): def printMessage(self): print('Messge from child.') super().printMessage() Child().printMessage()
Output
Messge from child. Messge from parent.
Notes
The super()
function can only be used inside a class function. The
following example throws an exception.
Example
Python
Copy Code
class Parent: def printMessage(self): print('Messge from parent.') class Child(Parent): def printMessage(self): print('Messge from child.') Child().super().printMessage()Output:
Traceback (most recent call last): File "BuiltInFunctions.py", line 9, in <module> Child().super().printMessage() AttributeError: 'Child' object has no attribute 'super'