April 2014
Beginner to intermediate
634 pages
15h 22m
English
As we look at the factory functions for creating Card objects, we see some alternative designs for the Card class. We might want to refactor the conversion of the rank number so that it is the responsibility of the Card class itself. This pushes the initialization down into each subclass.
This often requires some common initialization of a superclass as well as subclass-specific initialization. We need to follow the Don't Repeat Yourself (DRY) principle to keep the code from getting cloned into each of the subclasses.
The following is an example where the initialization is the responsibility of each subclass:
class Card: pass class NumberCard( Card ): def __init__( self, rank, suit ): self.suit= suit self.rank= ...