Chapter 8. The rules (and exceptions) of inheritance
This chapter covers
- Using inheritance and composition together to model systems
- Using Python built-ins to inspect object types
- Making interfaces more strict with abstract base classes
If you’ve written your own classes or used a class-based framework in Python, you’ve almost certainly encountered inheritance. Classes can inherit from other classes, ending up with their parent class’s data and behavior. In this chapter, you’ll learn the details of inheritance in Python, where it works well, and where it should be avoided.
Inheritance was conceived of in the early days of computer programming, but although it has existed for a long time, folks still have spirited debates about when and how it should be used. For much of the history of object-oriented programming, inheritance was the name of the game. Many applications sought to model the real world as a carefully curated hierarchy of objects, in the hopes that it would lead to some kind of obvious, neat structure. This paradigm was so embedded into object-oriented programming practices that the two concepts—object-oriented programming and inheritance—were nearly inseparable.
Though inheritance is sometimes the right tool to reach for, it has been used on occasion as the hammer for every nail—the elusive “silver bullet.” Much like a silver bullet, however, a paradigm that meets every need is a work of fiction.