How to do it...

We'll leverage the __slots__ special name when creating the class:

  1. Define the class with a descriptive name:

        class Hand: 
  1. 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.

  1. 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 ...

Get Modern Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.