Super keyword in Python? | Example

There is no super keyword instead Super function is used in Python to give access to methods and properties of a parent or sibling class.

Syntax

super() 

This function returns an object that represents the parent class.

Example Super keyword function in Python

Simple example codes the class that will inherit all the methods and properties from another class. Python subclasses can use the super keyword when calling the members of the parent class.

class Parent:

    def __init__(self, msg):
        self.message = msg

    def display(self):
        print(self.message)


class Child(Parent):
    def __init__(self, msg):
        super().__init__(msg)


obj = Child("Hello, Super!")

obj.display()

Output:

Super keyword in Python

Do comment if you have any doubts and suggestions on this Python keyword tutorial.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.