5. Dataclasses In Python
By Bernd Klein. Last modified: 19 Feb 2025.
Journey through Dataclasses: Advanced Techniques with Python Dataclasses
We delve into the powerful world of Python dataclasses in this chapter of our Python tutorial. Dataclasses are an essential feature introduced in Python 3.7 to simplify the creation and management of classes primarily used to store data. The chapter begins by pointing out the the difference of traditional class structures for representing data and introducing dataclasses as an attractive alternative. We will explore the syntax and functionalities of dataclasses and we will demonstrate how code readability will be enhanced.
Readers will gain a comprehensive understanding of the principles underlying dataclasses and will be able to applicate them in their Python projects.
Live Python training
See our Python training courses
First Examples
In our first example, we remain true to our beloved robot Marvin. So, we start with a "traditional" Python class Robot_traditional representing a Robot:
class Robot_traditional:
def __init__(self, model, serial_number, manufacturer):
self.model = model
self.serial_number = serial_number
self.manufacturer = manufacturer
The boilerplate code within the __init__ method, as seen in various class definitions, follows a similar pattern to this example, with the only variation being the names assigned to attributes. This becomes particularly tedious as the number of attributes grows.
If we have a look at the same example in dataclass notation, we see that the code gets a lot leaner. But before we can use dataclass as a decorator for our class, we have to import it from the module dataclasses.
from dataclasses import dataclass
@dataclass
class Robot:
model: str
serial_number: str
manufacturer: str
Here, the dataclass decorator automates the generation of special methods like __init__, reducing the need for boilerplate code. The class definition is concise, making it clearer and more maintainable, especially as the number of attributes increases.
This example demonstrates how using dataclasses for a class primarily used to store data, such as a robot representation, offers a more streamlined and readable alternative to traditional class structures.
Initializing robots of both classes is the same:
x = Robot_traditional("NanoGuardian XR-2000", "234-76", "Cyber Robotics Co.")
y = Robot("MachinaMaster MM-42", "986-42", "Quantum Automations Inc.")
Yet, there are other differences in these classes. The class decorator dataclass has not only created the special method __init__ but also __repr__, __eq__, __ne__, and __hash__. Methods which you would have to add manually to the call Robot_traditional.
Let's have a look at __repr__ and compare it to the traditional class definition:
print(repr(x))
print(repr(y)) # uses __repr__
OUTPUT:
<__main__.Robot_traditional object at 0x7f21c0acb410> Robot(model='MachinaMaster MM-42', serial_number='986-42', manufacturer='Quantum Automations Inc.')
We can see that __repr__ has been also implicitly installed by dataclass. To get the same result for Robot_traditional, we have to implement the method explicitly:
class Robot_traditional:
def __init__(self, model, serial_number, manufacturer):
self.model = model
self.serial_number = serial_number
self.manufacturer = 