CKTIS / Python
dataclasses.make_dataclass
Constructs a data class dynamically from a class name and field specifications.
Version: Python 3.14; repository image, production runtime not attested
Syntax
dataclasses.make_dataclass(cls_name, fields, *, decorator=dataclasses.dataclass)Constructs a data class dynamically from a class name and field specifications.
A field specification can supply a default. Positional construction still follows field declaration order.
Build a class dynamically from an iterable of field specifications. A bare name uses Any, a pair supplies the annotation, and a third component can configure a field or supply its default. The sample's y defaults to zero but can be overridden positionally. In 3.14 decorator can receive the generated class and dataclass options. module sets the generated class's __module__; it defaults to the calling module name.
from dataclasses import dataclass, field, make_dataclass
def tagged(cls, **options):
cls.tag = "generated"
return dataclass(cls, **options)
Point = make_dataclass("Point", [("x", int), ("y", int, field(default=0))], decorator=tagged)
assert Point(4).y == 0 and Point(4, 8).tag == "generated"
assert Point.__module__ == __name__
assert make_dataclass("P", [], module="records").__module__ == "records"
Examples
import dataclasses
Point = dataclasses.make_dataclass("Point", [("x", int), ("y", int, 0)])
print(Point(4), Point(4, 8))
Result
Point(x=4, y=0) Point(x=4, y=8)
Build a class dynamically from an iterable of field specifications. A bare name uses Any, a pair supplies the annotation, and a third component can configure a field or supply its default. The sample's y defaults to zero but can be overridden positionally. In 3.14 decorator can receive the generated class and dataclass options. module sets the generated class's `__module__`; it defaults to the calling module name.