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.

class created with dataclass decorator

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

We can now pass the student object to the dir() method to see the list of functions and attributes created.

print(dir(student))--------------------------------------------------------------------['__annotations__', '__class__', '__dataclass_fields__', '__dataclass_params__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name']

It comes with the __dict__ method

You can also use the __dict__ method on the object to get the instance attributes and their values as a dictionary.

__dict__ method created by dataclass

The output of the above code is

{'name': 'Tom', 'age': 20}

It comes with the string representation of the object

The __repr__ or __str__ dunder method representation usually gives a readable value for the instance of a class. The default format for the string representation of an instance of a class is

classname(arg1=val, arg2=val, ... argn=val)
__repr__ method created by dataclass

The output of the above code is

Student(name='Tom', age=20)

How to set default values for the arguments?

We can specify the default value for the attributes by the following syntax.

attribute: type = default_value

In the below example, created_at is provided with a default value and the current date is set as its value when an instance of the class is created.

providing default values to the arguments

The output of this code is

Student(name='Tom', age=20, added_at=datetime.date(2022, 7, 26))

What if you wish to customise these dunder methods?

There can be scenarios where you wish to customise the automatically created dunder methods by creating the dunder method of your own. dataclass also provides a solution to this problem.

You can provide the method that you wish to override/customise by providing the method name as an argument to the dataclass decorator with the value as false. This implicitly says that “do not create that particular dunder method for me”. You can also simply create your own dunder methods without setting them to false in the decorator. This will also produce the same behaviour.

The below example shows us how to override the __repr__ method.

custom __repr__ method

The output of this code snippet is

Tom: 20


  1. https://docs.python.org/3/library/dataclasses.html