Problem Formulation: Calling Parent Methods
Problem: Within a method of class
C, how to call the parents 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’
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:
Method 2: Use super() to Access Parent’s Method
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
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
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.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.