Understanding Python Class Inheritance: Five Common Techniques
This article explains the fundamentals of Python class inheritance, covering five typical ways to use parent class attributes and methods—including direct calls, super() for private members, method overriding, invoking __init__, and passing initialization parameters—accompanied by clear code examples.
Object‑Oriented Programming (OOP) relies heavily on inheritance, allowing a subclass to reuse and extend the functionality of an existing class without rewriting it.
1. Directly calling parent attributes and methods
The subclass can access public attributes and invoke methods defined in the parent class.
class Father():
def __init__(self):
self.money = 1000
def action(self):
print('Calling parent method')
class Son(Father):
pass
son = Son() # inherits all attributes and methods
son.action() # output: Calling parent method
print(son.money) # output: 10002. Using super() to call a parent’s private members
Private attributes (prefixed with double underscores) are name‑mangled and cannot be accessed directly; super() can invoke a private method but not private variables.
class Father():
__money = 1000 # private variable, not inherited
def __action(self):
print('Calling parent method')
class Son(Father):
def action(self):
super()._Father__action() # call private method
# print(self.__money) would raise an error
son = Son()
son.action() # output: Calling parent method
# NameError: name 'money' is not defined3. Overriding parent methods
The subclass can provide its own implementation of a method, replacing the parent’s version.
class Father():
def __init__(self):
self.money = 0
def action(self):
print('Calling parent method')
class Son(Father):
def __init__(self):
self.money = 1000
def action(self):
print('Subclass overrides parent method')
son = Son()
son.action() # output: Subclass overrides parent method
print(son.money) # output: 10004. Calling the parent’s __init__ method
If the parent initializes attributes in __init__ , the subclass can invoke it (with or without super() ) to inherit those attributes.
class Father():
def __init__(self):
self.money = 1000
class Son(Father):
def __init__(self):
super().__init__() # or Father.__init__(self)
son = Son()
print(son.money) # output: 10005. Inheriting parent initialization parameters
The subclass can pass arguments to the parent’s constructor and add its own parameters.
class Father():
def __init__(self, a, b):
self.earn_money = a
self.spend_money = b
def add(self):
return self.earn_money + self.spend_money
class Son(Father):
def __init__(self, a, b, c=1000):
Father.__init__(self, a, b)
self.son_money = c
def add(self):
return self.earn_money + self.spend_money + self.son_money
son = Son(1000, -500)
print(son.add()) # output: 1500These examples cover the basic ways to inherit and interact with parent class attributes and methods in Python, providing a solid foundation for writing your own OOP code.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.