Python Data Classes: Write Less Boilerplate

Python's @dataclass decorator writes boilerplate code like __init__ and __repr__ for you, turning a class with type hints into a data container. Use it for API payloads or simple records.
Why it exists
Before Python 3.7, creating classes just to hold data required manually writing init, repr, eq, and other special methods. This was repetitive and error-prone. Data classes were introduced to automate this boilerplate, making code cleaner and more maintainable.
The mental model
Think of the @dataclass decorator as a code generator for your classes. You provide a blueprint—a class with typed attributes—and the decorator automatically builds the standard machinery for initializing, representing, and comparing objects based on those attributes.
How it works
You apply the @dataclass decorator above a class definition. The decorator inspects the class for attributes that have type annotations (e.g., name: str). From these fields, it generates methods. By default, it adds init (to create instances), repr (for readable print outputs), and eq (for value-based comparison). You can customize its behavior with arguments, such as @dataclass(frozen=True) to make instances immutable or @dataclass(order=True) to enable sorting.
When to use it
Use data classes when your class's primary purpose is to store and group data, with little to no custom behavior. They are perfect for representing structured records like API responses, database rows, or configuration settings. They make your intent clear: this class is a data container.
When not to use it
If your class requires complex initialization logic, has significant behavior (many methods), or needs to manage intricate state, a regular class is often a better choice. While you can add methods to a data class, if the class is more about behavior than data, the @dataclass decorator adds little value and can be confusing.
One canonical example
To create a class for an inventory item, you simply define its fields with type hints: from dataclasses import dataclass; @dataclass class InventoryItem: name: str; unit_price: float; quantity_on_hand: int = 0. This small definition automatically provides a full init method, so you can create instances like item = InventoryItem('widget', 3.0, 10). It also generates a helpful repr, so printing the object shows InventoryItem(name='widget', unit_price=3.0, quantity_on_hand=10), and an eq method for comparisons.
Interview question
For which scenario would a Python dataclass be the most suitable choice?
- a.Designing a class that requires a custom __init__ method to handle unique object creation logic.
- b.Building a class that manages intricate state transitions and complex business rules.
- c.Developing a class where instances must be immutable by default without any explicit configuration.
- d.Creating a simple data structure to represent an API response with predefined fields.Correct
Why? this is the answer
Dataclasses are designed for classes whose primary purpose is to store and group data, making them perfect for structured records like API responses. They are not recommended for classes with complex logic, intricate state, or custom initialization needs, which are better handled by regular classes.
Just read this? Test yourself on what you have been reading.
Read the original → docs.python.org
- #python
- #data structures
- #decorators
- #oop
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.
See open roles