Explain Codes LogoExplain Codes Logo

What are data classes and how are they different from common classes?

python
data-classes
object-comparison
python-3-7
Nikita BarsukovbyNikita Barsukov·Mar 10, 2025
TLDR

Data classes: introduced by @dataclass, are Python's gift to those creating classes primarily for storing data. By auto-generating common methods like __init__ and __eq__, the need for boilerplate is greatly reduced.

from dataclasses import dataclass @dataclass class Item: name: str stock: int = 0 price: float = 0.0 # Let's create a shiny new gadget! item = Item("Gadget", stock=50, price=19.99) # 50 indeed! We're not messing around

Advantages: No more lonely nights spent writing boilerplate code, auto-generated methods like your own personal Python butler, and code readability for data-focused classes that zooms off the charts.

Savvy introduction to @dataclass

Kick your boilerplate blues to the curb with the @dataclass decorator. Often with classes in Python that focus on storing data, you'll find yourself writing boilerplate code for methods like __init__, __repr__, and __eq__. With @dataclass, just like an automated car factory, Python does the grunt work for you.

Trading complexity for simplicity

As Pythonistas, we love simplicity, and data classes are here to give us more of it. They bring default values, enforceable type annotations, and auto-generated comparison methods like __eq__, __lt__, etc., making object comparisons in Python a breeze.

Control over mutability

The key difference you'll notice between data classes and namedtuples is flexibility around mutability. By default, data classes are mutable, but they can mimic the immutability of namedtuples when desired, using the frozen parameter of the @dataclass decorator.

A closer look: Regular classes vs data classes

Regular classes are the marathons where state and behavior need to go hand-in-hand. Data classes, on the other hand, are your short, snappy sprints when you need a simple data structure and efficient ways to store and access it.

Leveraging hashability

With hashability, data classes can steal the limelight in sets or as dictionary keys, boasting fast lookup times and efficient data retrieval. But remember, to join the exclusive hashable club, your data class needs to be immutable.

Data class inspiration: The 'attrs' backstory

The attrs project, an efficient, boilerplate-busting tool inspired the creation of data classes. @dataclass comes with added brownie points as it's part of Python's standard library from version 3.7 onwards, making third-party dependencies a thing of the past.

Embracing limitations, exploring alternatives

Every superhero has a weakness and data classes are no different. Their limitations pop up when dealing with complex nested structures or the need for *args. In such cases, our old pals, the traditional classes or perhaps other patterns, would serve you better.

Real-world application: Data classes in coding challenges

In competitive programming battlegrounds like Advent of Code, the prowess of data classes is evident. With their simplicity and efficiency, you can focus on outsmarting the algorithm, while data classes handle the class creation mechanics.

Data classes: Your new secret learning weapon

To truly master data classes, start exploring the wonders of open-source projects on GitHub, or join the legions of Python meetups and conferences where you can learn directly from seasoned veterans.

Why coders love data classes

Data classes made object attribute comparison a piece of cake with the advent of dir() and inspect. Also, their primary use as data storage or transfer objects can help fellow coders understand your code's purpose faster than you can say "Python".