data class

In Python, a data class is a special type of class that you can create using the dataclasses module. They’re a handy way to store and manage data but also support mostly the same functionalities of regular classes, including user-defined methods.

In practice, data classes automatically implement common special methods like .__init__(), .__repr__(), .__eq__(), and others, saving you from writing the related boilerplate code and preventing potential associated errors.

Example

Here’s a quick example showing how to define and use a data class:

Python
>>> from dataclasses import dataclass

>>> @dataclass
... class Book:
...     title: str
...     author: str
...     year: int
...

>>> book = Book("Python Tricks: The Book", "Dan Bader", 2017)
>>> book.author
'Dan Bader'

Notice how you didn’t need to write the .__init__() or .__repr__() methods manually—the @dataclass decorator did it for you!

Tutorial

Data Classes in Python (Guide)

Learn how a Python dataclass reduces boilerplate, adds type hints and defaults, supports ordering and frozen instances, and still plays well with inheritance.

intermediate python stdlib

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Oct. 15, 2025