Problem Formulation: Calling Parent Methods
Given
Problem: Within a method of class C
, how to call the parents method P.my_method()
?
Example: Consider the following example where you create a parent and a child class. You want to call my_method(
) from within the child classes’ child_method(
).
class P: '''Parent Class''' def my_method(self): print('hi') class C(P): '''Child Class''' def child_method(self): # ... Call my_method() of parent P ... # TODO # Create a child child = C() child.child_method() # OUTPUT: hi
So, how to call parent methods? Let’s dive right into our first method!
Method 1: Use Self-Reference to Access Parent Method
The child inherits all methods and attributes from the parent. Thus, to access the parent method, you can use the self
reference passed as a child method’s argument. For example, you can call self.parent_method()
in the child method to access the parent method.
Here’s an example:
class P: def parent_method(self): print('hi') class C(P): def child_method(self): self.parent_method() child = C() child.child_method()
The output is, indeed:
hi
However, this only works if the child doesn’t overwrite the parent’s method. Read on to learn how to use super()
to handle this problem!
Method 2: Use super() to Access Parent’s Method
Python’s built-in super()
method returns a temporary object of the parent class to help you access its methods. Its purpose is to avoid using the base class name explicitly. It also enables your class to inherit from multiple base classes.
Here’s how you can ensure to call the parent’s method from the child using super()
—even if the child overwrites the same method.
class P: '''Parent''' def my_method(self): print('parent') class C(P): '''Child''' def my_method(self): print('child') def call_parent_method(self): super().my_method() child = C() child.call_parent_method() # parent
Note that if you didn’t use used super().my_method()
but self.my_method()
such as in Method 1, Python would call the child’s method as it overwrites the parent’s method.
class P: '''Parent''' def my_method(self): print('parent') class C(P): '''Child''' def my_method(self): print('child') def call_parent_method(self): self.my_method() child = C() child.call_parent_method() # child
You can watch the video to learn everything about Python’s super()
method:
Programmer Humor
There are only 10 kinds of people in this world: those who know binary and those who don’t.
👩🧔♂️
~~~
There are 10 types of people in the world. Those who understand trinary, those who don’t, and those who mistake it for binary.
👩🧔♂️👱♀️
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.