Python: Why DataClasses are Awesome
In this article I will discuss about the python dataclasses, where we can use these and how are they helpful. And also will explain this topic with examples, which will help you to getting started with it and use it in day to day coding.
What is DataClasses?
Data classes are bread and butter tool for everyday programmer. It can save you literally hours every week of writing boiler plate code instead of just showing you the syntax.
Data Classes are mainly aimed at helping you write more data oriented class. They simply act as containers of data, used by other classes.
As of python 3.7, a new exciting feature was introduced, the
@dataclass
decorator viaDataclasses
library.The
@dataclass
decorator is used to automatically generate base functionalities to classes, including__init__()
,__hash__()
,__repr__()
and more, which helps reduce some boilerplate code.
Installing the DataClasses module:
pip install dataclasses
Syntax:
@dataclasses.dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False)