dataclasses

The Python dataclasses module provides functionality for creating and working with user-defined data classes. These types of classes are mainly used to store data as attributes (fields) but can provide more functionality.

Data classes also provide default implementations for some common special methods, including .__init__(), .__repr__(), .__eq__(), and .__hash__(). This behavior saves you from writing the associated boilerplate code.

Here’s a quick example:

Python
>>> from dataclasses import dataclass

>>> @dataclass
... class Point:
...     x: int
...     y: int
...

>>> point = Point(1, 2)
>>> point
Point(x=1, y=2)

Key Features

  • Automatically generates special methods like .__init__(), .__repr__(), and .__eq__()
  • Supports default values and default factories for fields
  • Allows customization of field behavior with field metadata
  • Provides a way to create immutable data classes

Frequently Used Classes and Functions

Object Type Description
@dataclasses.dataclass Decorator Decorates a class to turn it into a data class
dataclasses.field() Function Customizes individual fields of a data class
dataclasses.asdict() Function Converts a data class instance to a dictionary
dataclasses.astuple() Function Converts a data class instance to a tuple

Examples

Creating a data class with default values for fields:

Python
>>> from dataclasses import dataclass

>>> @dataclass
... class Circle:
...     radius: float = 1.0
...

>>> circle = Circle()
>>> circle.radius
1.0

Converting a data class to a dictionary:

Python
>>> from dataclasses import asdict, dataclass

>>> @dataclass
... class Point:
...     x: int
...     y: int
...

>>> point = Point(5, 6)
>>> asdict(point)
{'x': 5, 'y': 6}

Common Use Cases

  • Facilitating the creation of classes that are mainly used for storing data
  • Automatically generating common methods like .__init__(), .__repr__(), and .__eq__()
  • Converting data class instances to dictionaries or tuples for serialization

Real-World Example

Suppose you need to manage a collection of employees with attributes like .name, .age, and .position. You can use dataclasses as shown below:

Python
>>> from dataclasses import dataclass

>>> @dataclass
... class Employee:
...     name: str
...     age: int
...     position: str
...

>>> employees = [
...     Employee("Alice", 30, "Developer"),
...     Employee("Bob", 25, "Designer"),
... ]

>>> for employee in employees:
...     print(employee)
Employee(name='Alice', age=30, position='Developer')
Employee(name='Bob', age=25, position='Designer')

In this example, the dataclasses module streamlines the creation and management of employee records.

Tutorial

Data Classes in Python 3.7+ (Guide)

Data classes are one of the new features of Python 3.7. With data classes you do not have to write boilerplate code to get proper initialization, representation and comparisons for your objects.

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated June 26, 2025