We'll leverage the __slots__ special name when creating the class:
- Define the class with a descriptive name:
class Hand:
- Define the list of attribute names:
__slots__ = ('hand', 'bet')
This identifies the only two attributes that are allowed for instances of this class. Any attempt to add another attribute will raise an AttributeError exception.
- Add an initialization method:
def __init__(self, bet, hand=None): self.hand= hand or [] self.bet= bet
Generally, each hand starts as a bet. The dealer then deals two initial cards to the hand. Under some circumstances, though, we might want to rebuild a Hand object from a sequence of Card instances. We've used a feature of the or operator. If the left side operand is ...