Understanding Python Class Inheritance, Subclassing, and Multiple Inheritance
This article explains Python's inheritance mechanism, showing how subclasses can reuse and extend parent class functionality, demonstrates the syntax for single and multiple inheritance, and illustrates method resolution order with clear code examples.
Python supports three core OOP features—encapsulation, inheritance, and polymorphism; this section focuses on inheritance, which allows a new class to reuse and extend the behavior of an existing class without copying code.
For example, a Shape class provides a draw() method. To create a Form class that can both draw and calculate area, the naïve approach would duplicate draw() and add an area() method.
class Shape: def draw(self, content): print("画", content) class Form: def draw(self, content): print("画", content) def area(self): # .... print("此图形的面积为...")
Python makes this easier by using inheritance: Form can inherit from Shape , automatically gaining the draw() method while only defining its own area() method.
class Shape: def draw(self, content): print("画", content) class Form(Shape): def area(self): # .... print("此图形的面积为...")
In this example, Form is the subclass (or child class) and Shape is the parent class (also called base class or super‑class).
The general syntax for inheritance is:
class ClassName(Parent1, Parent2, ...): # class body
If a class does not explicitly inherit from another class, Python implicitly inherits from the built‑in object class. Python also supports multiple inheritance, allowing a subclass to inherit from several parent classes.
Consider a Person class that inherits from both People and Animal :
class People: def say(self): print("我是一个人,名字是:", self.name) class Animal: def display(self): print("人也是高级动物") class Person(People, Animal): pass zhangsan = Person() zhangsan.name = "张三" zhangsan.say() zhangsan.display()
Running the code produces:
我是一个人,名字是:张三 人也是高级动物
Even though Person is defined with no body, it acquires all attributes and methods from both parent classes, demonstrating that inheritance brings in functionality automatically.
Python’s method‑resolution order (MRO) follows the order of parent classes listed; in the example, People appears before Animal , so if both define a method with the same name, the version from People is used.
Multiple inheritance can lead to complexity, especially when parent classes define methods with identical names. Python resolves such conflicts by giving precedence to the left‑most parent in the class definition.
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.