Skip to content

Python call super method | Example code

  • by

The super() function lets you run a parent class function inside the child class. Python super method returns a proxy object that delegates method calls to a parent or sibling class of type. It is used to access inherited methods that have been overridden in a class.

Python call super method example

Simple example code.

class Parent(object):
    def __init__(self, age):
        self.age = age

    def func(self):
        print(f"Hi, my age is {self.age}!")

class Child(Parent):
    def __init__(self, age):
        # use the super to run the parent class __init__ function to set the childs' name

dad = Parent(50)
kid = Child(18)

kid.func()  # The kid inherits it from the dad, so I could run it like that too


Python call super method

How do I call a parent class’s method from a child class in Python?

Answer: Use the super() function: Return a proxy object that delegates method calls to a parent or sibling class of type.

class A(object):
    def foo(self):
        print ("foo")

class B(A):
    def foo(self):
        super(B, self).foo()  # calls ''

myB = B()

Output: foo

How to call a super method from the grandchild class?

Answer: Here is an example of calling a super method from the grandchild class.

class Grandparent(object):
    def my_method(self):

class Parent(Grandparent):
    def my_method(self):

class Child(Parent):
    def my_method(self):
        print("Hello Grandparent")

obj1 = Child()


Hello Grandparent

Do comment if you have any doubts or suggestions on this Python method 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

Your email address will not be published. Required fields are marked *