How to Explicitly Pass Class and Instance to super() in Python

By IncludeHelp Last updated : February 21, 2024

Problem statement

Write Python code to explicitly pass class and instance to super().

Explicitly passing class and instance to super()

You can explicitly pass the current class and instance as arguments to change the behavior of super() method. Consider the below given code.

Python Code to Explicitly Pass Class and Instance to super()

# Create a parent class
class Class1:
    def __init__(self):
        print("Class1 constructor")

# Create an intermediate class
class Class2(Class1):
    def __init__(self):
        super(Class2, self).__init__()
        print("Class2 constructor")

# Create a child class
class Class3(Class2):
    def __init__(self):
        super(Class3, self).__init__()
        print("Class3 constructor")

# Main code
child_instance = Class3()

Output

The output of the above code is:

Class1 constructor
Class2 constructor
Class3 constructor

To understand the above program, you should have the basic knowledge of the following Python topics:

 
 

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.