The language feature most often associated with object-oriented programming is inheritance. Inheritance is the ability to define a new class that is a modified version of an existing class. It is called ‘inheritance’ because the new class inherits the methods of the existing class. Extending this metaphor, the existing class is called the parent class and the new class is called the child.
As an example, let’s say we want a class to represent a ‘hand’, that is, the set of cards held by one player. A hand is similar to a deck: both are made up of a set of cards, and both require operations like adding and removing cards. A hand is also different from a deck; there are operations we want for hands don’t make sense for a deck. For example, in poker we might compare two hands to see which wins. In bridge, we might compute a score for a hand in order to make a bid. This relationship between classes – similar, but different – lends itself to inheritance.
The definition of a child class is like other class definitions, but the name of the parent class appears in parentheses. The definition indicates that Hand inherits from Deck; that means we can use methods like pop_card and add_card for Hands as well as Decks. Hand also inherits the init method from Deck, but it doesn’t really do what we want: instead of populating the hand with 52 new cards, the init method for hands should initialize cards with an empty list. If we provide an init method in the Hand class, it overrides the one in the Deck class:
class Hand(Deck): """represents a hand of playing cards""" def __init__(self,label=‘‘): self.cards = [] self.label = label
The next natural step is to encapsulate this code in a method called move_cards inside class Deck:
def move_card(self,hand,num): for i in range(num): hand.add_card(self.pop_card())
move_cards takes two arguments, a Hand object and the number of cards to deal. It modifies both self and hand, and returns None.
In some games, cards are moved from one hand to another, or form a hand back to the deck. You can use move_cards for any of these operations: self can be either a Deck or a Hand, and hand, despite the name, can also be a Deck.
Write a Deck method called deal hands that takes two parameters, the number of hands and the number of cards per hand, and that creates new Hand objects, deals the appropriate number of cards per hand, and returns a list of Hand objects.
def deal_hands(self,num_hands,num_cards): hands = list() for i in range(num_hands): hand = Hand() self.move_card(hand,num_cards) hands.append(hand) return hands
from Thinking in Python