🎓 The Dataclasses Module¶
dataclasses
is a module for creating classes which are primarily used to store data. These classes, called data classes, have automatically added special methods, including __init__()
, __repr__()
, and __eq__()
.
1. Creating a Data Class¶
Here is how you can define a simple data class:
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
This Point
class has two fields, x
and y
, both of type int
. The @dataclass
decorator automatically adds a number of special methods to the class, including a constructor function (__init__()
) to initialize the data attributes:
p = Point(1, 2)
print(p) # Outputs: Point(x=1, y=2)
Point(x=1, y=2)
2. Default Field Values¶
You can specify default values for fields:
from dataclasses import dataclass
@dataclass
class Point:
x: int = 0
y: int = 0
When you create an instance of this class, you can omit values for any of the fields to use their default values:
p = Point()
print(p) # Outputs: Point(x=0, y=0)
Point(x=0, y=0)
3. Adding Methods¶
Like any other classes, data classes can have methods:
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
def distance_from_origin(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
You can call this method like any other method:
p = Point(3, 4)
print(p.distance_from_origin()) # Outputs: 5.0
5.0
4. Immutable Data Classes¶
You can make a data class immutable (i.e., read-only) by setting the frozen
parameter of the @dataclass
decorator to True
:
from dataclasses import dataclass
@dataclass(frozen=True)
class Point:
x: int
y: int
This will prevent modifying the fields of instances of the class:
p = Point(1, 2)
p.x = 3 # Raises dataclasses.FrozenInstanceError
--------------------------------------------------------------------------- FrozenInstanceError Traceback (most recent call last) Cell In[8], line 2 1 p = Point(1, 2) ----> 2 p.x = 3 # Raises dataclasses.FrozenInstanceError File <string>:4, in __setattr__(self, name, value) FrozenInstanceError: cannot assign to field 'x'
Data classes make it easier to create classes that are mainly used to store data. They automatically generate special methods that are commonly used in such classes, reducing the boilerplate involved.