Robots Atlas>ROBOTS ATLAS

Python — From Basics to Advanced · Dataclasses and Enums

@dataclass — Classes Without Boilerplate

Dataclasses and Enums

Introduction

The @dataclass decorator (PEP 557, Python 3.7+) generates __init__, __repr__, and __eq__ from type annotations. A 30-line record class becomes three lines. Modes: default (mutable), frozen=True (immutable, hashable), slots=True (3.10+, saves memory, blocks new attributes), kw_only=True (3.10+, all fields become keyword-only), order=True (generates <, <=, >, >= based on the value tuple). The __post_init__ hook runs validation after __init__. Helper functions: asdict, astuple, replace, fields. Mutable defaults (list, dict, set) are forbidden — use field(default_factory=...) instead.