Skip to main content

dataclasses

A decorator that synthesises __init__, __repr__, __eq__, ordering, hash, slots, and replacement on a regular Python class. Decorator-generated code is appended to the class at decoration time; introspection sees real methods.

Source-of-record: Lib/dataclasses.py, dataclasses docs.

@dataclass

@dataclass(*, init=True, repr=True, eq=True, order=False,
unsafe_hash=False, frozen=False, match_args=True,
kw_only=False, slots=False, weakref_slot=False)
ParameterDefaultEffect
initTrueGenerate __init__.
reprTrueGenerate __repr__.
eqTrueGenerate __eq__.
orderFalseGenerate <, <=, >, >=.
unsafe_hashFalseForce __hash__ even when mutable.
frozenFalseImmutable: assignments raise FrozenInstanceError.
match_argsTrueGenerate __match_args__ (PEP 634).
kw_onlyFalseAll fields keyword-only by default.
slotsFalseGenerate __slots__ and return a new class.
weakref_slotFalseAdd __weakref__ to slots.

Fields

field(*, default=MISSING, default_factory=MISSING, init=True,
repr=True, hash=None, compare=True, metadata=None,
kw_only=MISSING)
ParameterEffect
defaultStatic default value.
default_factoryZero-arg callable producing a default per instance.
initInclude in __init__.
reprInclude in __repr__.
compareUse for __eq__ / ordering.
hashOverride; default tracks compare.
metadataArbitrary mapping for downstream tools.
kw_onlyOverride the class-level setting per field.

The sentinel dataclasses.MISSING indicates "no value supplied".

Inspection

FunctionReturns
fields(class_or_instance)Tuple of Field objects in declaration order.
asdict(obj, *, dict_factory=dict)Recursively convert to dict.
astuple(obj, *, tuple_factory=tuple)Recursively convert to tuple.
is_dataclass(obj)Predicate.
replace(obj, /, **changes)New instance with fields replaced.
make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False)Build a dataclass at runtime.

Field attributes: name, type, default, default_factory, repr, hash, init, compare, metadata, kw_only.

Frozen

Frozen instances raise FrozenInstanceError on assignment. __hash__ is generated from the compare fields. Cycle: frozen=True and eq=True together produce a hashable value class.

Slots

slots=True rewrites the class with __slots__ set to the field names. Because __slots__ cannot be added after class creation, the decorator returns a fresh class object. Subclasses must also set __slots__ to keep instances slot-only.

Post-init hook

__post_init__(self) runs at the end of the generated __init__. Use to derive fields from inputs. With init=False, the user provides the __init__ and __post_init__ is not called.

InitVar

from dataclasses import InitVar
@dataclass
class C:
x: int
y: InitVar[int] = 0
def __post_init__(self, y): ...

InitVar[T] fields are passed to __init__ and __post_init__ but are not stored on the instance.

ClassVar

typing.ClassVar[T] annotations are excluded from the field set; they remain ordinary class attributes.

Gopy status

AreaState
@dataclass and all optionsComplete.
field, MISSINGComplete.
fields, asdict, astuple, replace, make_dataclassComplete.
__post_init__, InitVar, ClassVarComplete.
slots=True, weakref_slot=TrueComplete.

Reference

  • CPython 3.14: dataclasses.
  • Lib/dataclasses.py.
  • module/dataclasses/. gopy port.
  • PEP 557.