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:
>>> 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!
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- Object-Oriented Programming (OOP) in Python (Tutorial)
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Using Data Classes in Python (Course)
- Data Classes in Python (Quiz)
- Intro to Object-Oriented Programming (OOP) in Python (Course)
- Object-Oriented Programming (OOP) in Python (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Python Classes - The Power of Object-Oriented Programming (Quiz)
By Leodanis Pozo Ramos • Updated Oct. 15, 2025