ID EN
Dataclasses

dataclass

Python 3.11

This function is a decorator that is used to add generated special methods to classes, as described below.

Syntax

PYTHON
@dataclasses.dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False)

Examples

Example 1
PYTHON
@dataclass
class C:
    ...

@dataclass()
class C:
    ...

@dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False,
           match_args=True, kw_only=False, slots=False, weakref_slot=False)
class C:
    ...
Example 2
PYTHON
@dataclass
class C:
    a: int        'a' has no default value
    b: int = 0    assign a default value for 'b'
Example 3
PYTHON
def __init__(self, a: int, b: int = 0):

See Also

decorator special methods type annotation __init__() __repr__() __eq__() __lt__() __le__()