1 | initial version |
In Python, we can mimic calls to the parent class using the super()
function. This function allows us to invoke the method of the parent class from within a child class.
Here's an example of how we can use super()
to mimic calls to the parent class:
class Parent:
def greet(self):
print("Hello from Parent")
class Child(Parent):
def greet(self):
super().greet()
print("Hello from Child")
# Create an instance of Child and call its greet method
c = Child()
c.greet()
Here, we define two classes Parent
and Child
. The Child
class inherits from the Parent
class. The Parent
class has a greet
method that simply prints a greeting message. The Child
class overrides the greet
method and first calls the greet
method of the parent class using super().greet()
, and then prints its own greeting message.
When we create an instance of the Child
class and call its greet
method, we get the following output:
Hello from Parent
Hello from Child
So, we see that by using super()
to invoke the method of the parent class, we can mimic calls to the parent class in Python.