How to call parent class method in a sub class of python
- User
How to call parent class method in a sub class of python
- Assistant
In Python, you can call a method from a parent class using the
super()function or directly by using the parent class name. Here's how you can do it both ways:Using
super()In Python,
super()is typically used to call methods from the parent class. This is the preferred method as it automatically handles the method resolution order in multiple inheritance scenarios.pythonclass Parent: def greet(self): print("Hello from the Parent class") class Child(Parent): def greet(self): print("Hello from the Child class") super().greet() # Call the greet method from the Parent class child = Child() child.greet()