Python Data Class: A Better Way to Store Data

A Python data class is a regular Python class that has the @dataclass decorator and is specifically created to hold data. Since Python version 3.7, Python offers data classes through a built-in module called dataclass. There are several advantages over regular Python classes which we’ll explore in this article. We’ll also look at example code and a couple of common operations you might want to perform with data classes.

The advantage of using data classes

Why should you use a data class instead of a regular Python class? First, let’s look at some of the advantages a Python data class has to offer.

Requires a minimal amount of code

The @dataclass decorator adds a lot of functionality to a class without adding any visible code. This allows your data class to be very compact while still offering many useful features. All you need to do is define the fields to hold your data. You don’t need to define any function.


Two Python data classes can be compared with == because the so-called dunder method __eq__ is implemented automatically. Any Python object that implements this special method can be compared to similar objects.

Printing a data class

Similarly, because __repr__ is implemented, you can print data classes and get a nice representation of it. This is especially useful for debugging.

Data classes require type hints

Data classes are built around the new(ish) type system Python offers. Using type hints reduces the chances of bugs and unexpected behavior in your code. You essentially declare the type of data that should be stored in a variable.

Python data class example

Here’s an example of a data class at work:

from dataclasses import dataclass

class Card:
    rank: str
    suit: str
card1 = Card("Q", "hearts")
card2 = Card("Q", "hearts")

print(card1 == card2)
# True

# 'Q'

Card(rank='Q', suit='hearts')

Default values

A data class can have default values. Assigning default values is as simple as assigning a value to a variable. For example, to make our Card class have a default value of Queen of hearts, we can do as follows:

from dataclasses import dataclass

class Card:
    rank: str = 'Q'
    suit: str = 'hearts'

Converting a data class to JSON

The bad news: there’s no built-in way to convert a data class to JSON. Luckily, there is a Python package called dataclasses-json that simplifies the task. However, it requires an extra decorator. Also, the package doesn’t seem to be actively maintained. Something to keep in mind.

Here’s an example of how the package can be used:

from dataclasses import dataclass
from dataclasses_json import dataclass_json

class Card:
    rank: str = 'Q'
    suit: str = 'hearts'

card = Card()
{"rank": "Q", "suit": "hearts"}

You’ll need to install the package with Pip or Pipenv, preferably inside a virtual environment.

Another method is to use Python inheritance and inherit from the JSONEncoder class to create your own custom encoder. The advantage here is that you don’t need to install an external package. How to do this is explained in a blog post.

Keep learning

About the author

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python!

1 thought on “Python Data Class: A Better Way to Store Data”

  1. I am in the banking industry for almost 30 years, quite a long time of working experience. Now, prior to retirement, I am beginning to start a new career as a lecturer. I want to share my skill, knowledge, and experiences to the millennials.
    During my work in banking industry, several times I develop electronic banking products, but my position always in the product design side, or on user’s perspective, and never doing the coding. My roles was always on writing the functional specification document.
    I ever learn Basic language, but never been used. I want to learn Python to find new experiences, and perhaps something amazing might come out of it.

Leave a Comment