Member-only story

Why you should use the dataclass from python

Allwin Raju
3 min readJul 26, 2022

--

Photo by ThisisEngineering RAEng on Unsplash

Python provides us with a useful module called dataclasses. If the primary purpose of your class is just to store data, then you should definitely consider using the dataclasses module from python.

Why should you use it?

The key advantage of using a dataclass is that it automatically creates all the dunder methods for you thus reducing all the boilerplate code.

Let us get right in.

To use dataclasses import dataclass from the dataclasses module and decorate the class with that.

You don’t need the __init__ constructor

You can simply go ahead and create an object just like you would normally do. By default, this is true and will be created automatically. You can also define your own __init__ method.

Object creation

--

--

No responses yet