PyDocs

Nav

Python Runtime Services

field

dataclasses

function · Added in 3.10 (source)
  • field(*, default=MISSING, default_factory=MISSING, init=True, repr=True, hash=None, compare=True, metadata=None, kw_only=MISSING, doc=None)
Return an object to identify dataclass fields.

default is the default value of the field.  default_factory is a
0-argument function called to initialize a field's value.  If init
is true, the field will be a parameter to the class's __init__()
function.  If repr is true, the field will be included in the
object's repr().  If hash is true, the field will be included in the
object's hash().  If compare is true, the field will be used in
comparison functions.  metadata, if specified, must be a mapping
which is stored but not otherwise examined by dataclass.  If kw_only
is true, the field will become a keyword-only parameter to
__init__().  doc is an optional docstring for this field.

It is an error to specify both default and default_factory.
Changed in 3.15
  • 3.15: If metadata is None, use an empty frozendict, instead of a MappingProxyType() of an empty dict.
0 Examples
No examples for dataclasses/field.
See Also
No see-alsos for dataclasses/field
0 Notes
No notes for field