Short Guide to Python Dataclasses- 2

Published on:
August 10, 2022

What are dataclasses? While we can take a step back to read our guide. But what next? Well, why should you use the python dataclass? Simply the reason is to increase the code's efficiency and decrease the boilerplate code c.

In this blog, MarsDevs illustrates the reason why you should use Python Dataclasses.

We will go through the reasons one by one now:

  • More compact code to define a class.

When we define a class with some attributes, it looks like this. 

It’s a standard Python syntax.
It’s a standard Python syntax.

But if you want to use dataclasses, you must import them first and then use them as a decorator before defining the class. Here’s the previous code you saw after you used dataclasses. 

Well, you can observe quite a few changes here now.
Well, you can observe quite a few changes here now.

There are fewer boilerplates here, and compared with the code above, you don’t need to do the repetitive task of defining each attribute.

Also, you can see the use of type annotation for each attribute in the code above, which helps your text editor, like if you use any type checker like mypy, provide a better linting.

  • Default values.

Assigning default values to a class is just similar to assigning values to any variable in a program.

No writing of comparison methods.
  • No writing of comparison methods.

If you write a simple python code to check the data within the attributes of a particular class whether they are equal or not, then it would be like this.

           It is pretty obvious that the two are not equal, is it not!!
           It is pretty obvious that the two are not equal, is it not!!

Well yes, it is pretty obvious because the ‘person’ class doesn’t implement any particular method that would check for equality of the two objects. Hence to do the same, you will have to exclusively add a method called __eq__, which would obviously make you write more code.

Also if you change any of the attributes in your class then you will have to update the __eq__ method again.

The same goes with other methods like __gt__,__ge__ etc., if used.

Fortunately, dataclasses remove this extra work. Let's see how. 

You can figure out the changes, I guess.
You can figure out the changes, I guess.
  • Initiating immutable objects.

Using dataclasses you can initiate an object with constant values of the attributes present in it. All you need to do is that you have to set the frozen argument to true inside the @dataclass decorator. 

And one cannot simply change them because that will show an error called FrozenInstanceError.

 A sample code to show the same.

 A sample code to show the same.

Get tech-savvy with MarsDevs

You should just leave the technologically challenging days in the past. You are here now in this discussion with MarsDevs about Python DataClasses. It is time to take a step back from ignorance and get tech-savvy. And we can make this investment affordable for you. 

With more and more innovative start-ups coming up, you make or break it in this industry now. Technology helps you be more efficient. And every business is tech-based. So, they would disrupt inefficiency. Being tech-savvy is not just a tool anymore. It decides your journey. 

MarsDevs is the one-stop development shop that takes care of all your tech needs. Proud of our excellent team of developers, we present you with skills, expertise, and overall tech assistance. We can chip in anytime you want, from consultancy to development, deployment to maintenance. But, let's not get late. Let us know your requirements as we book you a slot to discuss our expertise.


Similar Posts