PyDocs

Nav

Python Runtime Services

make_dataclass

dataclasses

function (source)
  • 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, weakref_slot=False, module=None, decorator=dataclass)
Return a new dynamically created dataclass.

The dataclass name will be 'cls_name'.  'fields' is an iterable
of either (name), (name, type) or (name, type, Field) objects. If type is
omitted, use the string 'typing.Any'.  Field objects are created by
the equivalent of calling 'field(name, type [, Field-info])'.::

  C = make_dataclass('C', ['x', ('y', int), ('z', int, field(init=False))], bases=(Base,))

is equivalent to::

  @dataclass
  class C(Base):
      x: 'typing.Any'
      y: int
      z: int = field(init=False)

For the bases and namespace parameters, see the builtin type() function.

The parameters init, repr, eq, order, unsafe_hash, frozen, match_args, kw_only,
slots, and weakref_slot are passed to dataclass().

If module parameter is defined, the '__module__' attribute of the dataclass is
set to that value.
Changed in 3.14
  • 3.14: Added the decorator parameter.
0 Examples
No examples for dataclasses/make_dataclass.
See Also
No see-alsos for dataclasses/make_dataclass
0 Notes
No notes for make_dataclass