AppDividend
Latest Code Tutorials

Python Super Function Example | Super() Method Tutorial

0

Python Super Function Example | Super() Method Tutorial is today’s topic. The super() builtin returns the proxy object that allows you to refer parent class by ‘super.’ The super function in Python can be used to gain access to inherited methods which is either from a parent or sibling class. 

The super() builtin function returns a proxy object, a substitute object a substitute object that can call the method of the base class via delegation. This is called indirection (ability to reference base object with super()). The super function is versatile and can be used in the couple of ways.

If you want to learn more about Python then check out this course Learn Python Programming Masterclass

Python Super Function Example

If we want to understand the python super function, we need to know about Inheritance in the Python language. In Python Inheritance, the subclasses can inherit from the superclass.

Python super function can refer the superclass implicitly. So, Python super () function makes our task more manageable.

While referring the superclass from the base class, we don’t need to write the name of superclass explicitly. 

In the following sections, we will discuss the python super function.

How to Call Super in Python 3?

We can call using the following syntax. We will take a regular class definition and modify it by adding the super function. The final code with super() keyword looks like below.

class MyParentClass():
    def __init__(self):
        pass

class SubClass(MyParentClass):
    def __init__(self):
        super()

As you can see, this is a setup of single inheritance.

We can see that there’s the base or parent class (also sometimes called the superclass), and derived class or subclass.

But we still need to initialize the parent or base class within the subclass or derived or child. 

We can call the super() function to process easier.  The goal of Super function is to provide a much more abstract and portable solution for initializing classes.

Let’s see the example of the super() function in Python.

# app.py

class Computer():
    def __init__(self, computer, ram, ssd):
        self.computer = computer
        self.ram = ram
        self.ssd = ssd

class Laptop(Computer):
    def __init__(self, computer, ram, ssd, model):
        super().__init__(computer, ram, ssd)
        self.model = model

lenovo = Laptop('lenovo', 2, 512, 'l420')
print('This computer is:', lenovo.computer)
print('This computer has ram of', lenovo.ram)
print('This computer has ssd of', lenovo.ssd)
print('This computer has this model:', lenovo.model)

In the above example, we have defined one base class which is Computer, and one is derived class which is Laptop.

We have defined three properties inside the base class, and the derived class has total four properties. 

Three properties from derived class are derived from the base class, and fourth is that’s own property. In the derived or child class has its model property. Other three are obtained from base class Computer.

So, now if we only create an object of the derived class, we still have all the access of the base class’s property because of super() function.

The output of the above example is the following.

 

Python Super Function Example

Python super() function with multilevel inheritance

As we have stated previously that Python super() function allows us to refer the superclass implicitly.

But in the case of multi-level inheritances which class will it refer? Well, a Python super() will always refer the immediate superclass.

Also, a Python super() function not only can refer the __init()__ function but also can call all other function of the superclass. So, in the following example, we will see that.

See the following example.

# app.py

class A:
    def __init__(self):
        print('Initializing: class A')

    def sub_method(self, b):
        print('Printing from class A:', b)


class B(A):
    def __init__(self):
        print('Initializing: class B')
        super().__init__()

    def sub_method(self, b):
        print('Printing from class B:', b)
        super().sub_method(b + 1)


class C(B):
    def __init__(self):
        print('Initializing: class C')
        super().__init__()

    def sub_method(self, b):
        print('Printing from class C:', b)
        super().sub_method(b + 1)


if __name__ == '__main__':
    c = C()
    c.sub_method(1)

See the output below.

 

Super() Method Tutorial Example

So, from the output we can clearly see that the __init()__ function of class C had been called at first, then class B and after that class A. 

The same thing happened by calling sub_method() function.

If your program contains multi-level inheritance, then this super() function is beneficial for you.

Finally, Python Super Function Example is over.

Leave A Reply

Your email address will not be published.

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