8.1 Choosing between inheritance and composition – the ”is-a” question
In the Using cmd to create command-line applications recipe in Chapter 6, and the Extending a built-in collection – a list that does statistics recipe in Chapter 7, we looked at extending a class. In both cases, the class implemented in the recipe was a subclass of one of Python’s built-in classes.
The idea of extension via inheritance is sometimes called the generalization-specialization relationship. It can also be called an is-a relationship. There’s an important semantic issue:
Do we mean that instances of the subclass are also instances of the superclass? This is an is-a relationship, an example of inheritance, where we extend a class, changing the implementation details of features.
Or do we mean something else? Perhaps there’s a composition or association, sometimes called a has-a relationship. In this case...