2-Minute Guide: dataclasses in Python
Python's @dataclass decorator simplifies the creation of classes that mainly hold data. Let's compare a regular class with a @dataclass to see the benefits.
Regular Class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f"Person(name={self.name}, age={self.age})"
As you can see, we need to manually define the __init__ and __repr__ methods.
@dataclass
python
from dataclasses import dataclass
@data
---
*Follow me on Dev.to for daily Python tips and quick guides!*
Top comments (0)