How Dataclass May Beautify Your Python Code

How Dataclass May Beautify Your Python Code

Figure out how to represent elegantly data structures with ease

Guillaume Vincent
Guillaume Vincent

Dataclasses are among the new features introduced by Python 3.7. Its goal is to simplify the class creation containing mainly data without functionality. This can be compared to commonly used data structures.

When using a class as a data structure, we have to implement the same methods as __init__() and __repr__(), which is cumbersome. Dataclasses autogenerates these methods for us. This offers more transparency in the language.

Declaration

The declaration of a data class is done by affecting a decorator to a class. In the example below, we will create a data class to represent GPS coordinates:

You can directly print the Position object without having to define a __repr__() function:

$ python dataclass_ex1.py
Position(lat=37.6216, lon=-122.3929)

We can compare dataclass objects with the equal operator without having to implement an extra __eq__ method :

$ python dataclass_ex2.py
True

Methods

You can add methods inside a dataclass. We are going to implement a method to compute the Haversine distance in kilometers between two positions :

$ python dataclass_ex3.py
15479.614752629424

Dataclass object as method attribute

The dataclass object is considered as any other Python type. We implement another class to instantiate Town objects. Basically, a Town object will have a position attribute :

Inheritance

dataclass supports inheritance as well. The Town class presented in the last section can be simplified. We will consider a town as position. Town objects will inherit from latitude and longitude attributes :

To go further we add a new class to distinguish the capital among the towns :

Fields

Data class can use the field() specifier to customize each field individually. The field supports many different parameters. We will use some of them to define default values for fields and metadata for the longitude and the latitude:

Immutability

dataclass offers an immutability option setting frozen=True. When it’s enabled, the dataclass fields may never change. Be careful of the nested dataclass that contains immutable fields using inheritance.

We will add a new Country class to have a collection of the different towns of a country (including the capital). In this class, there is a function get_capitalthat filters the capital data from the list of the towns:

Conclusion

We’ve seen that by using dataclass, you don’t need to write repetitive methods each time you write a class. These methods are already implemented and completely transparent. You can implicitly define them to override the default behavior.

Furthermore, we’ve realized that dataclass is really similar to a classic class and supports inheritance. You can as well define class attributes from an existing dataclass. Finally, you can define immutable objects if you feel it is appropriate to your concerns.

Through examples, we can assert that dataclass is an elegant solution to create comprehensive data models.

Since I’ve discovered this feature, I try to use it the most possible for great readable code! And you?

Resources

PEP 557 -- Data Classes
The official home of the Python Programming Language
dataclasses — Data Classes — Python 3.9.4 documentation
Programming

Guillaume Vincent

DevOps Engineer & AWS Certified Solution Architect. Cloud enthusiast and automation addict