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 implements common special methods like .__init__(), .__repr__(), .__eq__(), and others, saving you from writing the related boilerplate code and previenting 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 3.7+ (Guide)

Data classes are one of the new features of Python 3.7. With data classes you do not have to write boilerplate code to get proper initialization, representation and comparisons for your objects.

intermediate python

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


By Leodanis Pozo Ramos • Updated June 20, 2025