Skip to content

.tip

· one of the small things

dataclass(slots=True, frozen=True) for value objects

Slots cut memory per instance by about half, and turn an attribute typo into an error where it used to silently create a new attribute. Frozen makes the object hashable and safe to share.

@dataclass(slots=True, frozen=True)
class Money:
    amount: int
    currency: str

Use both flags by default for anything that represents a value, as opposed to an entity with an identity.

python