Python Tip #241 (of 365): Let dataclasses write your dunder methods. The datacla...
Trey Hunner ๐ @treyhunner@mastodon.social
Python Tip #241 (of 365):
Let dataclasses write your dunder methods.
The dataclass decorator writes __init__, __repr__, and __eq__ for you:
from dataclasses import dataclass
@dataclass
class Comment:
text: str
line: int
Back in tips 63, 64, and 66 I recommended giving every class an initializer, a friendly string representation, and (sometimes) a custom sense of equality.
That's three methods of boilerplate for even a simple class:
๐งต (1/3)