Sometimes, we want to call a parent class’s method from a child class in Python.
In this article, we’ll look at how to call a parent class’s method from a child class in Python.
How to call a parent class’s method from a child class in Python?
To call a parent class’s method from a child class in Python, we can use super to access the parent class from the child class.
For instance, we write
class Foo(Bar):
def baz(self, **kwargs):
return super().baz(**kwargs)
to create the Foo class that’s a subclass of the Bar class.
Then we can call the Bar class’ baz method in the Foo class’ baz method with super().baz(**kwargs).
kwargs has all the keyword arguments in a dict.
And we use ** to unpack them as arguments.
Conclusion
To call a parent class’s method from a child class in Python, we can use super to access the parent class from the child class.