Chapter 4. When Objects Are Alike
In the programming world, duplicate code is considered evil. We should not have multiple copies of the same, or similar, code in different places.
There are many ways to merge pieces of code or objects that have a similar functionality. In this chapter, we'll be covering the most famous object-oriented principle: inheritance. As discussed in Chapter 1, Object-oriented Design, inheritance allows us to create is a relationships between two or more classes, abstracting common logic into superclasses and managing specific details in the subclass. In particular, we'll be covering the Python syntax and principles for:
- Basic inheritance
- Inheriting from built-ins
- Multiple inheritance
- Polymorphism and duck typing
Basic inheritance
Technically, every class we create uses inheritance. All Python classes are subclasses of the special class named object
. This class provides very little in terms of data and behaviors (the behaviors it does provide are all...