7.6 Using frozen dataclasses for immutable objects
In the Using typing.NamedTuple for immutable objects recipe, we saw how to define a class that has a fixed set of attributes. The attributes can be checked by the mypy program to ensure that they’re being used properly. In some cases, we might want to make use of the slightly more flexible dataclass to create an immutable object.
One potential reason for using a dataclass is because it can have more complex field definitions than a NamedTuple subclass. Another potential reason is the ability to customize the initialization and the hashing function that is created. Because a NamedTuple is essentially a tuple, there’s limited ability to fine-tune the behavior of the instances in this class.
7.6.1 Getting ready
We’ll revisit the idea of defining simple playing cards with rank and suit. The rank can be modeled by an integer between 1 (ace) and 13 (king.) The suit can be...