Skip to content

Archive

Dataclasses

2 articles
Python 04 Sep 2026 8 min read

Use Slotted Dataclasses When Object Shape Is Fixed in Python

Python dataclasses reduce boilerplate for record-like classes, but their instances are still ordinary Python objects by default. Declared fields normally live in an instance dictionary, and new attributes can be attached later. That flexibility is useful until the model is supposed to have a fixed shape. Coordinates, parsed records, configuration snapshots, protocol messages, and other compact value objects often have a known set of fields. When many such objects exist, keeping dynamic per-instance attribute storage may also be unnecessary.

Python 01 Sep 2026 3 min read

Design Value Objects with Python Dataclasses

Python dictionaries are convenient for passing a few values around, but they become fragile when a value has invariants or behavior that deserves a name. dataclasses can express small domain value objects without repetitive constructors and representation methods. Start with domain meaning A price represented as a dictionary is easy to misuse: price = {"amount": 1299, "currency": "USD"} Any caller can omit a key or accidentally mix cents and dollars. A value object makes the contract explicit: