dataclass module is introduced in Python 3.7 as a utility tool to make structured classes specially for storing data. These classes hold certain properties and functions to deal specifically with the data and its representation.
DataClasses in widely used Python3.6
Although the module was introduced in Python3.7, one can also use it in Python3.6 by installing dataclasses library.
pip install dataclasses
DataClasses are implemented by using decorators with classes. Attributes are declared using Type Hints in Python which is essentially, specifying data type for variables in python.
GfgArticle(title=’DataClasses’, author=’vibhu4agarwal’, language=’Python’, upvotes=0)
The two noticeable points in above code.
- Without a __init__() contructor, the class accepted values and assigned it to appropriate variables.
- The output of printing object is a neat representation of the data present in it, without any explicit function coded to do this. That means it has a modified __repr__() function.
The dataclass provides an in built __init__() constructor to classes which handle the data and object creation for them.
TypeError: __init__() missing 4 required positional arguments: ‘title’, ‘author’, ‘language’, and ‘upvotes’
We can also modify the functioning of in-built constructor by passing certain arguments or using special functions which will be discussed in further articles.
Equality of DataClasses
Since the classes store data, checking two objects if they have the same data is a very common task that’s needed with dataclasses. This is accomplished by using the == operator.
Below is the code for an equivalent class for storing an article without a dataclass decorator.
DataClass Equal: True Normal Class Equal: False
Equality between two objects using == operator in python checks for the same memory location. Since two objects take different memory locations on creation, the output for equality is False. Equality between DataClass objects checks for the equality of data present in it. This accounts for True as output for equality check between two DataClass objects which contain same data.
- Data Classes in Python | Set 4 (Inheritance)
- Data Classes in Python | Set 6 (interconversion to and from other datatypes)
- Data Classes in Python | Set 2 (Decorator Parameters)
- Data Classes in Python | Set 3 (dataclass fields)
- Data Classes in Python | Set 5 (post-init)
- Abstract Classes in Python
- Multiprocessing in Python | Set 1 (Introduction)
- Python | Introduction to Matplotlib
- Python Language Introduction
- Introduction to Convolutions using Python
- NumPy in Python | Set 1 (Introduction)
- Python | Introduction to PyQt5
- Python sorted containers | An Introduction
- Complex Numbers in Python | Set 1 (Introduction)
- Array in Python | Set 1 (Introduction and Functions)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.