Programming Languages

What is the purpose of the super function in Python?

Medium
2
Added
The super function is used to call methods from a parent class in a subclass. It is useful for ensuring that the parent class is properly initialized.

Solution Code

Programming Languages
class Parent:
    def __init__(self):
        print("Parent constructor")

class Child(Parent):
    def __init__(self):
        super().__init__()
        print("Child constructor")

# Example usage
child = Child()
Explanation
This code snippet demonstrates how to use the super function to call the parent class constructor.

Guided Hints

Define a parent class with a constructor
Define a child class that inherits from the parent
Use super to call the parent constructor