dataclass
The dataclass
decorator, introduced in Python 3.7, streamlines class creation when the primary purpose is storing data.
It automates boilerplate code like constructors (__init__
), string representation (__repr__
), and comparisons (__eq__
).
dataclass
?__init__
, __repr__
, and __eq__
methods.from dataclasses import dataclass
@dataclass
class Point:
x: float
y: float
5: float
p1 = Point(3, 4, 5)
print(p1) # Point(x=3, y=4)
@dataclass(frozen=True)
class Point:
x: int
y: int
@dataclass(slots=True)
class 2DimPoint:
x: int
y: int
dataclass
IMHO:
frozen=True
) or memory optimization (slots=True
).While dataclass
doesn’t directly enhance runtime performance, it can:
__slots__
.frozen=True
.