Python dataclasses and enums: clean data models

11 steps · about 3 h 42 min · Intermediate

  1. 9. Classes — Python 3.15.2 documentation (doc, docs.python.org, 25 min)

    Start with the class basics, including the official note on dataclasses as the idiomatic way to bundle named data.

  2. If You’re Not Using Python DATA CLASSES Yet, You Should 🚀 (video, ArjanCodes (YouTube), 12 min)

    A short, chaptered walkthrough of creating, sorting, defaulting and freezing dataclasses.

  3. Python dataclass (tutorial, pythontutorial.net, 15 min)

    Compares a hand-written class with a dataclass, then covers defaults and asdict/astuple conversion.

  4. Python dataclasses will save you HOURS, also featuring attrs (video, mCoding (YouTube), 15 min)

    A second view of dataclasses from a different teacher, plus a first look at attrs.

  5. using dataclass decorators in Python in custom classes (tutorial, ZetCode, 20 min)

    Practise the pitfalls: mutable defaults with default_factory, frozen and ordered classes, __post_init__, kw_only and slots.

  6. dataclasses — Data Classes — Python 3.15.2 documentation (reference, docs.python.org, 30 min)

    The official reference settles the details of field(), replace(), asdict() and the decorator options.

  7. enum (reference, Real Python, 10 min)

    Introduces enumerations: replacing magic numbers and string constants with named members.

  8. Enum HOWTO — Python 3.15.2 documentation (doc, docs.python.org, 35 min)

    The authoritative guide to auto(), unique, Flag, IntEnum and the cookbook patterns.

  9. Python Enum: Enum Class, auto(), IntEnum, StrEnum, Methods, and Examples (guide, GoLinuxCloud, 20 min)

    Practical examples of StrEnum, enum methods, multi-field members and when an enum beats a dict.

  10. Attrs, Pydantic, or Python Data Classes: Which One Is The Best? (video, ArjanCodes (YouTube), 20 min)

    Finish by learning where dataclasses stop being enough and when to reach for attrs or Pydantic.

  11. ArjanCodes/2023-attrs (repo, GitHub (ArjanCodes), 20 min)

    Run the same model as a dataclass, attrs and Pydantic to compare them directly.