Dataclasses – Python

Dataclass is a type of class that introduced in Python 3.7 as part of the dataclasses module. It’s designed to simplify the creation of classes primarily used to store data. Dataclasses handles automaticly class-based constructors (such as init, repr, str, slots …) to specify data storing.

Basics of Dataclass

Dataclass is a decorator and built-in module with python 3.7+

To create new dataclass, use the @dataclass decorator, as follows:

Create a new dataclass object that named user_1:

Make a comparison between dataclass and standart-class to better understand the logic of dataclasses.

Althought there is no much data, name and surname variables are duplicated 3 times in the class. This will cause more complexity when dataclass is getting bigger and bigger.

Also, they will be different and cannot comparable even if we create same name and surname.

 For the standart-class User to imitate the dataclass User above, you need to refactor the class as well:

__repr__ constructor helps to return nice string represantation and __eq__ constructor provides class comparision.

To sum up, dataclasses handles contructors and converts class to data-storeable, automaticly.

Default Values

Defining default value is simple with dataclasses.

This operates equivalently to explicitly defining default values within the . __init__() method of a standard class.

Type Hints

Dataclass requires to define variable with type hint but if there is no clear data we can define Any type

As we see above, we can decide while defining hobit parameter and code runs without any problem.

Dataclass Methods

Dataclasses are specialized standard Python classes designed for storing data. So, we can add methods.

So, create_username() method works as one might anticipate.

Frozen Dataclasses

If we had a class that contains constant variables, we have to prevent change values. We can define a dataclass that contains constants and they are immutable.

Setting dataclass decorator’s parameter frozen=True will be enough for creating immutable dataclass.

and code block raises FrozenInstanceError as we expected.

Data Validation

In dataclass, the __post_init__ constructor is a special method that can be defined within a class, including dataclasses, to perform additional initialization or validation after the object has been created. So, we can use __post_init__ constructor to validate data.

As we expected, the code block raises error beceause of data validation failed.

Note: To validating data more advanced, use pydantic module.


Dataclasses in Python offer a streamlined approach to defining classes primarily intended for storing data, reducing boilerplate code and improving code readability. Introduced in Python 3.7, the dataclasses module provides a decorator that automatically generates special methods such as __init__, __repr__, __eq__, and __hash__, based on class attributes. This simplifies the creation of data-centric classes, allowing developers to focus on the structure and logic of their data rather than on repetitive implementation details. With features like default values, type hints, and support for inheritance, dataclasses provide a powerful tool for managing structured data in Python applications, enhancing productivity and maintainability.

ibrahim tunc
ibrahim tunc
Articles: 13

Leave a Reply

Your email address will not be published. Required fields are marked *