Size: 3104
Comment:
|
Size: 3089
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
## page was renamed from Python/DataClass = Python DataClass = |
= Python DataClasses = |
Line 4: | Line 3: |
'''`dataclass`''' is a module that supports the creation of simple classes. | '''`dataclasses`''' is a module that supports the creation of simple classes. |
Line 20: | Line 19: |
import dataclass | import dataclasses |
Line 22: | Line 21: |
@dataclass.dataclass | @dataclasses.dataclass |
Line 57: | Line 56: |
import dataclass | import dataclasses |
Line 59: | Line 58: |
@dataclass.dataclass | @dataclasses.dataclass |
Line 78: | Line 77: |
import dataclass | import dataclasses |
Line 80: | Line 79: |
@dataclass.dataclass | @dataclasses.dataclass |
Line 86: | Line 85: |
dataclass.asdict(p) # {'x': 10, 'y': 20} | dataclasses.asdict(p) # {'x': 10, 'y': 20} |
Line 96: | Line 95: |
import dataclass | import dataclasses |
Line 98: | Line 97: |
@dataclass.dataclass | @dataclasses.dataclass |
Line 104: | Line 103: |
dataclass.astuple(p) # (10, 20) | dataclasses.astuple(p) # (10, 20) |
Line 142: | Line 141: |
import dataclass | import dataclasses |
Line 144: | Line 143: |
@dataclass.dataclass | @dataclasses.dataclass |
Python DataClasses
dataclasses is a module that supports the creation of simple classes.
Contents
Decorators
@DataClass
Use the @dataclass decorator on a class to automatically generate dunder methods.
import dataclasses @dataclasses.dataclass class User: name: str # 'name' has no defualt value id: int = 1000 # 'id' defaults to 1000
A TypeError is raised if a field without a default value follows a field with a default value.
Classes
Field
A Field object represents a field in a dataclass. For complex logic, a dataclass can be defined in terms of Field objects.
A Field object is instantiated with the field() function. This function takes the following optional named arguments:
default: default value
default_factory: callable that generates the default value as needed
init: boolean indicating that this field should be included in the generated __init__ method
repr: boolean indicating that this field should be included in the generated __repr__ method
hash: boolean indicating that this field should be included in the generated __hash__ method
compare: boolean indicating that this field should be included in the generated comparison methods (e.g. __eq__ and so on)
kw_only: boolean indicating that this field is keyword-only
A Field object has name and type atributes, as well as attribute matching the names and meanings of the field() function's arguments.
To create a dataclass with a field that defaults to an empty list, try:
import dataclasses @dataclasses.dataclass class User: groups: list[int] = dataclass.field(default_factory=list) u = User() u.groups += [1, 2, 3]
Functions
AsDict
import dataclasses @dataclasses.dataclass class Point: x: int y: int p = Point(10, 20) dataclasses.asdict(p) # {'x': 10, 'y': 20}
AsTuple
import dataclasses @dataclasses.dataclass class Point: x: int y: int p = Point(10, 20) dataclasses.astuple(p) # (10, 20)
Fields
Extract the Field objects from a dataclass.
Is_DataClass
Make_DataClass
Replace
Constants
KW_ONLY is a type annotation used for dataclasses.
import dataclasses @dataclasses.dataclass class Point: x: float _: dataclass.KW_ONLY y: float z: float p = Point(0, y=1.5, z=2.0)
MISSING is a sentinel value used internally.
See also
Python dataclasses module documentation