-
Book Overview & Buying
-
Table Of Contents
Modern Python Cookbook - Third Edition
By :
We’ve noted two general kinds of objects in Python:
Immutable: During design, we’ll ask if something has attributes with values that never change. If the answer is yes, see the Using typing.NamedTuple for immutable objects recipe, which offers a way to build class definitions for immutable objects.
Mutable: Will there be state changes for one or more attributes? In this case, we can either build a class from the ground up, or we can leverage the @dataclass decorator to create a class definition from a few attributes and type hints. This case is the focus of this recipe.
How can we leverage the dataclasses library to help design mutable objects?
We’ll look closely at a mutable object with an internal state to represent a hand of cards. While individual cards are immutable, they can be inserted into a hand and removed from a hand...