Have you seen a @dataclass
decorator in some python code and still don't understand what it does? Hang in there we are going to cover it here.
The @dataclass
decorator comes from from the dataclasses module and what this module does is to provide a decorator and functions for adding special/dunder methods to you class such as __init__()
and __repr__()
.
Let's start with a simple example here:
from dataclasses import dataclass
@dataclass
class Programmer:
"""Class for keeping track of programmer information."""
name: str
age: int
def total_friends(self) -> int:
return self.age - self.age
The above example will add a __init__()
method that looks like this:
def __init__(self, name: str, age: int):
self.name = name
self.age = age
This method is automatically added for you unless you specify it.
You might be wondering what might be the benefit of this, well to answer that let's look at what are special/dunder methods and what they do.
Special methods
Special/dunder methods are methods that are preceded and succeeded by double underscores, hence the name dunder. They are predominantly used for operator overloading which means that a predefined operator is provided with extended meaning.
Below is an example of operator overloading.
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
# this allows us to add Vectors
def __add__(self, other):
return self.__class__(self.x + other.x, self.y + other.y)
v1 = Vector(1, 4)
v2 = Vector(5, 3)
v3 = v1 + v2 # v3.x == 6 and v3.y == 7
The above __add__()
method allows us to add Vectors and if we try to add them without overriding add we will get an error.
The most recognized special method is the __init__()
method, which enables a class to be initialized with specific attributes.
class Programmer:
"""Class for keeping track of programmer information."""
def __init__(self, name: str, age: int):
self.name = name
self.age = age
To instantiate an instance of the Programmer
class, we must specify the instance's name and age. For example:
john = Programmer(name='John', age=29)
What dataclasses does for you
The dataclass()
decorator examines the class to find fields
. A field
is defined as a class variable that has a type annotation. The order of the fields
in all of the generated methods is the order in which they appear in the class definition.
The dataclass()
decorator also takes some arguments and below are the default arguments from the function's signature.
@dataclasses.dataclass(*, init=True, repr=True, eq=True, order=False,
unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False,
weakref_slot=False)
By default the dataclass()
decorator adds the __init__()
, __repr__()
and the __eq__()
methods.
In general data classes helps getting rid of boilerplate code. All the methods generated for you by the dataclass()
decorator can be overridden if you want to alter the behavior.
In my next post about data classes I will get into more advanced features and give a useful real world example.
Photo by Rubaitul Azad on Unsplash
Top comments (0)