Learn dataclasses

Generate __init__, __repr__, __eq__ and more for data-holding classes

13 lessons · 2 hours · 4 levels · covers dataclasses Python 3.13

The dataclasses module turns a class with annotated fields into a full-featured record type, generating the constructor, repr, comparisons, hashing and more. It removes boilerplate from configuration objects, domain models and DTOs while staying plain Python classes. Learning it well means concise, correct value types and knowing when to choose frozen, slotted or keyword-only variants.

Before you start

dataclasses builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 32 min in total
2. PracticalUse it productively at work, with the usual idioms7 lessons · 1 hour in total
3. AdvancedThe less common features, performance and trade-offs10 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know13 lessons · 2 hours in total

The dataclasses tree

dataclassesYour first dataclassYour first dataclassDefaults and default_factoryDefaults anddefault_factoryEquality and orderingEquality and orderingFrozen dataclassesFrozen dataclassesValidation and derived fields with __post_init__Validation andderived fields with…field() options and metadatafield() options andmetadataConverting and copying: asdict, fields, replaceConverting andcopying: asdict, fiel…Classes and instancesClasses andinstances
Essentials and Practical shown (7 of 13 lessons); the full list is below. Click a lesson to read it.

All 13 lessons, in order

Dataclass basics

  1. Your first dataclass · Essentials · 8 min

    Decorate a class with @dataclass to get __init__, __repr__ and __eq__ generated from its annotated fields.

  2. Defaults and default_factory · Essentials · 8 min

    Give fields default values, and use field(default_factory=list) for mutable defaults.

  3. Equality and ordering · Essentials · 8 min

    Understand dataclass equality and enable sorting and comparisons with order=True.

  4. Frozen dataclasses · Essentials · 8 min

    Make instances immutable with frozen=True, get hashability for free, and create modified copies with replace().

Fields and initialisation

  1. Validation and derived fields with __post_init__ · Practical · 10 min

    Run validation and compute derived attributes after the generated __init__ with __post_init__ and field(init=False).

  2. field() options and metadata · Practical · 10 min

    Control each field's role in __init__, repr, comparison and hashing with field(), and attach metadata.

  3. Converting and copying: asdict, fields, replace · Practical · 9 min

    Turn dataclasses into dicts and tuples for JSON, introspect them, and copy with replace().

Layout and inheritance

  1. slots=True and keyword-only fields · Advanced · 11 min

    Save memory and speed up attribute access with slots=True, and make constructors clearer with kw_only and KW_ONLY.

  2. Dataclass inheritance · Advanced · 11 min

    Extend dataclasses with subclasses, understand how fields are merged and ordered, and fix default-ordering errors.

  3. InitVar and ClassVar · Advanced · 9 min

    Pass init-only values to __post_init__ with InitVar, and keep class-level constants out of fields with ClassVar.

Internals and alternatives

  1. Hashing rules: eq, frozen and unsafe_hash · Expert · 12 min

    Know exactly when dataclasses generate, keep or remove __hash__, and why unsafe_hash is dangerous.

  2. How @dataclass generates code · Expert · 13 min

    Look inside the decorator: __dataclass_fields__, exec-generated methods, __match_args__, make_dataclass and descriptor fields.

  3. Dataclasses vs NamedTuple, attrs and Pydantic · Expert · 11 min

    Choose between dataclasses, NamedTuple, TypedDict, attrs and Pydantic, and know how dataclass_transform connects them to type checkers.