June 2019
Beginner to intermediate
770 pages
19h 24m
English
A composite object can also be called a container. We'll look at a simple composite object: a deck of individual cards. This is a basic collection. Indeed, it's so basic that we can, without too much struggle, use a simple list object as a deck.
Before designing a new class, we need to ask this question: is using a simple list object appropriate?
We can use random.shuffle() to shuffle the deck and deck.pop() to deal cards into a player's Hand.
Some programmers rush to define new classes as if using a built-in class violates some object-oriented design principle. Avoiding a new class leaves us with the following code snippet:
>>> d = [card(r + 1, s) for r in range(13) for s in iter(Suit)]>>> random.shuffle(d)>>> hand = [d.pop(), ...
Read now
Unlock full access