Separating concerns via multiple inheritance
In the Choosing between inheritance and extension – the is-a question recipe earlier in the chapter, we looked at the idea of defining a Deck
class that was a composition of playing card objects. For the purposes of that example, we treated each Card
object as simply having a rank and a suit. This created two small problems:
- The display for the card always showed a numeric rank. We didn't see J, Q, or K. Instead we saw 11, 12, and 13. Similarly, an ace was shown as 1 instead of A.
- Many games, such as Blackjack and Cribbage, assign a point value to each rank. Generally, the face cards have 10 points. For Blackjack, an ace has two different point values; depending on the total of other cards in the hand, it can be worth 1 point or 10 points.
Python's multiple inheritance lets us handle all of these variations in card game rules while keeping a single, essential Card
class. Using multiple inheritance...