Skip to content

Super keyword in Python? | Example

  • by

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

A 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 or suggestions on this Python keyword tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading