An Insight into Dataclasses in Python

Ever had a class used mostly to contain data, then had to implement a comparison, hashing and other dunder methods, and ended up with a pile of boilerplate code? That’s where Dataclasses make it all simpler and more compact.

Juan Velasquez
2 min readSep 1, 2021

Why Dataclasses?

Classes can be described as a mix of methods and attributes. Some classes work as containers for behavior, other classes act more as containers of data. The latter is the main topic of this article, classes that work as a blueprint to encapsulate data.

Dataclasses helps you initialize and fill an object with data, it gives you the tools to easily implement ordering, hashing, representation and more features that you can use with an object.

An example of Dataclasses’ magic

Let’s implement a class that represents a point in a Cartesian coordinate system using the conventional way. This point will be immutable and comparable with other points.

Now the same class, with the same features, but using Dataclasses.

So there you go, from 48 lines of code to 6 lines of code (counting blank lines for readability). Also, imagine you want to add a z attribute to represent a 3D point. Using the conventional way you would have to implement the new attribute in each of its methods, whereas with Dataclasses you would need to add just one more line.

Decorator’s parameters

As you can see in the example above, I added some parameters to the dataclass decorator, these parameters describe the nature of the class:

  • frozen (True by default) makes the objects immutable, and therefore they can be used as dictionary keys, which in my opinion is a very important property.
  • order (False by default) allows objects to be compared with other objects of the same type.
  • repr (True by default) will generate the __repr__ method with the format NameOfClass(attr1='value1', attr2='value2',...) .
  • init (True by default) takes care of the initialization of each attribute according to its declaration.
  • eq (True by default) implementes the __eq__ method that compares objects of the same type as tuples of its fields.
  • unsafe_hash (False by default) determines how __hash__ is implemented according to eq and frozen.

How to describe each attribute?

So, let’s see with an example how to set a default value, how to add a default mutable argument and more.

Conclusion

When dealing with data oriented classes Dataclasses are a great tool. It will avoid a lot of typing and thinking, making the code less error prone since it already does almost everything for you. Other methods can be added as well and used like in any other class, but it’s recommended to leave the basic stuff managed by Dataclasses.

There is a lot more to discuss about Dataclasses, so depending on the response this article gets, there will be more in-depth content coming.

--

--

Juan Velasquez

Software developer. Python. Automation tester. Microelectronics. Twitter: @__jvelasquez__