The
@dataclass decorator automatically generates standard methods like init, repr, and eq based on the class attributes.Example 👇
# with dataclass
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
Here's how it would look without
@dataclass:class Point:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
def __repr__(self):
return f"Point(x={self.x}, y={self.y})"
def __eq__(self, other):
return (self.x, self.y) == (other.x, other.y)
dataclass saves time and eliminates boilerplate code when a class simply stores data.
👉 @DataScience4
