What are dataclasses?
Dataclasses are a feature introduced in Python 3.7 that provides a way to simplify the creation of classes that are primarily used to store data.
It automatically generates special methods like __init__() based on the class attributes, reducing boilerplate code and making it easier to create classes that are primarily used for storing data.
Example
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
point1 = Point(1, 2)
print(point1) # Output: Point(x=1, y=2)TIP
Use them as a lightweight/built-in alternative to libraries like
Pydanticwhen you only need simple data structures.