A dataclass with validation
Python__post_init__ runs after the generated constructor, which is where cheap invariants belong.
from dataclasses import dataclass, field
@dataclass(frozen=True, slots=True)
class Money:
amount: int
currency: str = 'GBP'
def __post_init__(self):
if self.amount < 0:
raise ValueError('amount cannot be negative')