A dataclass defines the shape of a small record while generating routine initialization and representation methods.
Worked example
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
print(Point(3, 4))Result
Point(x=3, y=4)Implementation notes
Generated equality compares the declared fields by default. Consider whether operational metadata should participate when using instances as records in tests.
Try it yourself
Add a mutable list field with a default factory and compare two instances.
Open the Python example →Editorial date September 27, 2025 · Published December 6, 2026