Quick reference for the decorator parameters on @dataclass and the arguments to dataclasses.field(). For the tutorial treatment see the data classes notebook.
@dataclass(init=True,# generate __init__repr=True,# generate __repr__eq=True,# generate __eq__order=False,# generate __lt__, __le__, __gt__, __ge__unsafe_hash=False,# generate __hash__ even when it would be riskyfrozen=False,# make instances immutablematch_args=True,# (3.10+) expose __match_args__ for match/casekw_only=False,# (3.10+) all fields are keyword-onlyslots=False,# (3.10+) use __slots__weakref_slot=False,# (3.11+) add a __weakref__ slot)classThing:...
Parameter
Default
Effect
init
True
Generate __init__. Set False if you want to write your own.
repr
True
Generate __repr__.
eq
True
Generate __eq__ that compares all fields in order.
Force a generated __hash__ even when the combination of eq/frozen would normally leave hashing off. Rarely needed.
frozen
False
Make instances immutable. Assigning to a field raises FrozenInstanceError. Frozen classes get __hash__ automatically.
match_args
True
Generate __match_args__ (Python 3.10+) so the class can be used in match patterns.
kw_only
False
Make every field keyword-only. Useful when you have a lot of fields and want callers to name them at call sites.
slots
False
Generate a __slots__ class. Smaller instances, rejects undeclared attributes. Note: this replaces the class with a new one, so some metaclass or weakref tricks need extra care.
weakref_slot
False
With slots=True, reserve a slot for weak references.