Python dataclass: Taking advantage of the new decorator

Petros Demetrakopoulos
3 min readNov 18, 2022

--

Photo by Chris Ried on Unsplash

Introduction

Python 3.7 introduced a new and really interesting decorator called dataclass. Despite this decorator offers no new functionality, which means that it does not offer a new possibility to python developers in terms of implementation, it makes it way easier to define classes oriented to store data. Think of it like what we used to call “Plain Old JavaScript Objects” (aka POJOs) in Javascript. Classes that their role is mainly to store data and to contain minimal logic. Actually, there are no restrictions to how much or what kind of logic these classes can contain, however this is my personal design advice (and the way I believe this tool is designed to be used).

An example

A simple dataclass defining a User

As you may already understand from this simple example, an important aspect of dataclasses is that you get many functionalities “for free”, which means you do not have to implement them by hand. Functions like __init__ (class initializer), __repr__(function returning a string representation of the class) and __eq__ (function to compare various instances with each other) are already implemented.

The notation and the syntax is simple: After you declare a class under the “@dataclass” decorator, then you simple declare the fields of your class along with there respective data type.

Default values

Dataclasses also provide the functionality to declare default values for fields if you want to. Below you can see an example of a dataclass with default values for the fields name and surname. Note that in case you do not want to set default values for all fields, the fields that have default values must be declared at the end of the fields list.

Adding functions

Dataclasses as we said make no less things than normal python classes. With that being said, dataclasses can also accommodate functions. A simple example is shown below. note that a good architectural practice, is to only enclose functions within a dataclass that do not need access to data outside of a class instance.

Combining dataclasses

Finally, another strength of Python dataclasses is that they can be combined. Which means that you can create fields of types that you have declared earlier as a dataclass. An example of how this can be done is shown below:

Here, we see that the dataclass Order becomes the type of the list orders in another dataclass ( User ).

Conclusion

This tutorial aimed to briefly present some of the functionalities and use cases of the new Python dataclass decorator. This new addition is very powerful and useful as it provides a way to create better structured classes representing data. Of course, this is just a brief introduction. Dataclass capabilities are far more and beyond the scope of this article but I hope you got an idea and that you will probably consider using them in your next Python project.

--

--

Petros Demetrakopoulos

💻Code-blooded, 🌏 Traveler, . Lifelong learner 📚. Currently studying Data Science and AI at TU/e, Eindhoven, NL. https://petrosdemetrakopoulos.github.io