Glossary

Dataclass

A dataclass (introduced in PEP 557, Python 3.7) is a class decorated with @dataclasses.dataclass that auto-generates common dunder methods — __init__, __repr__, __eq__, and optionally __hash__, __lt__, and others — from type-annotated class attributes. Dataclasses reduce boilerplate for classes that primarily hold data, while remaining full classes that can have methods, properties, and inheritance.

The decorator reads the class's type annotations and generates an __init__ that accepts them as parameters: @dataclass class Point: x: float; y: float produces Point(x=1.0, y=2.0). Options include frozen=True (makes instances immutable and hashable), order=True (generates comparison methods), slots=True (uses __slots__ for memory efficiency), and field() for default values, default factories, and metadata.

Dataclasses occupy a middle ground between named tuples (immutable, lightweight) and full hand-written classes. They are mutable by default (unlike named tuples), support inheritance, and can have methods. The dataclasses.asdict() and dataclasses.astuple() functions convert instances to dictionaries and tuples for serialisation. For validated data (e.g., API input), Pydantic models offer similar syntax with runtime type validation. For simple immutable records, typing.NamedTuple may be lighter.

Related terms: Class, Named Tuple, Type Hint

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.