from dataclasses import dataclass
@dataclass
class Point:
x: int
y: intData Classes
Data classes is a feature in Python that allows creating a class that is designed to hold data. This saves from writing eloborate __init__ methods for all the fields.
The dataclasses are defined in the dataclasses module.
Introduction
We create a data class by decorating the class by specifying the type of each field and decorating the class with @dataclass decorator.
Once we declare a data class, it automatically get __init__ and __repr__ methods.
Point(10, 20)Point(x=10, y=20)
We can also create a Point by specifying the arguments by name.
Point(x=1, y=2)Point(x=1, y=2)
It is even possible to specify default values for the fields.
@dataclass
class Point:
x: int = 0
y: int = 0Point()Point(x=0, y=0)
Adding Methods
Like a regular class, we could add methods to a data class.
from dataclasses import dataclass
@dataclass
class Point:
x: int = 0
y: int = 0
def double(self):
x = self.x * 2
y = self.y * 2
return Point(x=x, y=y)Point(3, 4).double()Point(x=6, y=8)
Utilities
The dataclasses module provides some utilities to convert a data class into a dict.
from dataclasses import dataclass, asdict
@dataclass
class Point:
x: int = 0
y: int = 0p = Point(10, 20)
asdict(p){'x': 10, 'y': 20}
Discussion
The dataclasses reduce the barrier to create new classes. This is especially useful when working with APIs etc., where the code becomes a lot easy to read and understand because all the fields expected in the response are listed explicitly.