Mapping to a tuple of values

The following is the essence of how mapping is done to a two-tuple:

def card6(rank: int, suit: Suit) -> Card:
class_, rank_str = {
1: (AceCard, "A"),
11: (FaceCard, "J"),
12: (FaceCard, "Q"),
13: (FaceCard, "K")
}.get(
rank, (Card, str(rank))
)
return class_(rank_str, suit)

This is a reasonably pleasant design because it uses a simple mapping. It's not much code to handle special cases of playing cards. We will see how it could be modified or expanded if we needed to alter the Card class hierarchy to add additional subclasses of Card.

It does feel odd to map a rank value to a class object and one of the two arguments to that class initializer. It seems more sensible to map the rank to a simple class or function object without the clutter of providing some (but not all) of the arguments.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset