Frozen Dataclasses
Understand how to use frozen dataclasses in Python to create immutable objects. Explore the benefits of immutability such as safer inheritance, bug detection, and consistent hashing for duplicate detection.
We'll cover the following...
Overview
The general case for dataclasses is to create mutable objects. The state of an object can be changed by assigning new values to the attributes. This isn’t always a desirable feature, and we can make a dataclass immutable.
We can describe the UML diagram of the design by adding a stereotype of «Frozen». This notation can help to remind us of the implementation choice of making the object immutable. We must also respect an important rule of frozen dataclasses: an extension via inheritance must also be frozen.
The definition of the frozen Sample objects ...