liveBook · Manning

Dataclass

As explained in Python How-To

A data class is a Python class whose declared attributes are treated as fields and for which common methods such as a constructor can be generated automatically.

Need a class that mainly stores data? Writing the same constructor assignments and string representation for every class gets repetitive. Decorate the class with `@dataclass` to generate methods such as `__init__` and `__repr__` while keeping a custom class you can extend. section 9.2 section 9.2.5

Define a bill

```python from dataclasses import dataclass @dataclass class Bill: table_number: int meal_amount: float served_by: str tip_amount: float = 0 print(Bill(5, 60.5, "John")) # Bill(table_number=5, meal_amount=60.5, served_by='John', tip_amount=0) ``` The annotated names become fields. No explicit constructor or representation method is needed, and the default lets callers omit `tip_amount`. section 9.2.1 section 9.2.2

Annotations matter: `dataclass` uses the class’s `__annotations__` to find fields and generate the applicable methods. Put fields without defaults before fields with defaults, just as you would with function parameters. section 9.2.2

Choose field behavior to fit the data:

  • Leave the class mutable when instances need updating. Use `@dataclass(frozen=True)` when changes should be prevented; assigning to a field then raises `FrozenInstanceError`. section 9.2.3
  • To extend a data class, decorate the subclass too. Its constructor takes inherited fields before new fields. A defaulted superclass field cannot precede a required subclass field, so avoid superclass defaults when subclasses need that flexibility. section 9.2.4
  • For a mutable field such as a list, do not simply copy the ordinary default-value pattern; the `dataclasses.field` function is identified as the tool to investigate for that case. section 9.2.6

Use generated construction after JSON decoding

A task represented as a dictionary can become a `Task` instance through its generated constructor: `Task(**task_dict)`. A class method can wrap that conversion when decoded JSON must become objects with the class’s functionality rather than remain dictionaries. section 9.3.3

How Python How-To covers it

How deep it goes
Explains field discovery through annotations, defaults, freezing, and inheritance; leaves the mutable-field default solution as a challenge.
Examples
Uses restaurant bills to show generated methods and field options, then converts JSON-decoded task dictionaries into instances.
What it assumes
Assumes basic custom classes and decorators; teaches how annotated attributes become data-class fields.
What you can do after
Build it from scratch with `@dataclass`, choosing field defaults, frozen behavior, and subclass fields for a data model.
How it connects
Connects type annotations, decorators, special methods, inheritance, named tuples, and JSON deserialization.

Also explained in

Compare how 7 books explain dataclass →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.