DEV Community

qing
qing

Posted on

2-Minute Guide: dataclasses in Python (2026)

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})"
Enter fullscreen mode Exit fullscreen mode

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!*
Enter fullscreen mode Exit fullscreen mode

Top comments (0)