Skip to content

@dataclass writes the methods

Level: 201 → 301 · for Python programmers

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.

One line: @dataclass reads the class's annotations and writes __init__, __repr__ and __eq__ for you, and nothing else unless asked — no ordering without order=True, no hash unless frozen, no __slots__ without slots=True, no type check ever — while it refuses a mutable default and accepts a mutable field without a word.

What the finished page has to answer

  • What was written, shown with inspect.signature(C.__init__) and vars(C) before and after; and the rule that a field without a default cannot follow one with, which is TypeError at class creation.
  • field(): default_factory, init=False, repr=False, compare=False, kw_only=True; __post_init__ for the derived field.
  • frozen=True: assignment raises FrozenInstanceError, a subclass of AttributeError, and the hash comes back, which Defining __eq__ deletes __hash__ section 6 measures.
  • order=True compares field tuples in declaration order, and refuses to coexist with a hand-written __lt__.
  • slots=True (3.10) and what it changes about A class attribute is shared section 6.
  • asdict and astuple copy deeply, replace makes a new instance, and fields() is the list the decorator worked from, which is why an unannotated name is not in it.
  • Types are not checked, ever: C('a') when the field says int runs, which is Annotations are not checked from the class's side.

See also