Glossary

Inheritance

Inheritance is the object-oriented mechanism by which a new class (the subclass or child) derives attributes and methods from an existing class (the superclass or parent). In Python, inheritance is declared by listing parent classes in parentheses: class Dog(Animal):. The subclass inherits all methods and attributes of the parent and can override them to provide specialised behaviour.

Python supports multiple inheritance: a class can inherit from more than one parent. The Method Resolution Order (MRO), computed using the C3 linearisation algorithm, determines the order in which base classes are searched when looking up a method. You can inspect the MRO with ClassName.__mro__ or ClassName.mro(). The super() function follows the MRO to call the next class in the chain, enabling cooperative multiple inheritance.

While inheritance is a fundamental OOP concept, Python programmers tend to favour composition over inheritance — containing an object of another class rather than inheriting from it. This leads to more flexible, less tightly coupled designs. Abstract Base Classes (from the abc module) define interfaces that subclasses must implement, while Protocols (from typing) define structural subtyping without requiring inheritance at all.

Related terms: Class, Composition, Abstract Base Class, Method Resolution Order

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.