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 [] 

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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.