2. Use dataclass(slots=True) for simple records
For simple data containers, prefer @dataclass(slots=True) over writing repetitive boilerplate by hand.
This keeps the declaration compact while also preventing arbitrary attribute creation on instances. It is a good fit for lightweight records whose fields are known up front and whose main job is to hold data clearly.
Note
Python 3.10+
2.1. Don’t do this
1class Point:
2 def __init__(self, x, y):
3 self.x = x
4 self.y = y
5
6 def __repr__(self):
7 return f'Point(x={self.x}, y={self.y})'
2.2. Do this
1from dataclasses import dataclass
2
3@dataclass(slots=True)
4class Point:
5 x: int
6 y: int