Data Classes in Python | An Introduction
Last Updated :
23 Apr, 2021
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
The 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.
Python3
# A basic Data Class
# Importing dataclass module
from dataclasses import dataclass
@dataclass
class GfgArticle():
"""A class for holding an article content"""
# Attributes Declaration
# using Type Hints
title: str
author: str
language: str
upvotes: int
# A DataClass object
article = GfgArticle("DataClasses",
"vibhu4agarwal",
"Python", 0)
print(article)
Output:
GfgArticle(title='DataClasses', author='vibhu4agarwal', language='Python', upvotes=0)
The two noticeable points in above code.
- Without a __init__() constructor, 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.
Python3
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.
Python3
class NormalArticle():
"""A class for holding an article content"""
# Equivalent Constructor
def __init__(self, title, author, language, upvotes):
self.title = title
self.author = author
self.language = language
self.upvotes = upvotes
# Two DataClass objects
dClassArticle1 = GfgArticle("DataClasses",
"vibhu4agarwal",
"Python", 0)
dClassArticle2 = GfgArticle("DataClasses",
"vibhu4agarwal",
"Python", 0)
# Two objects of a normal class
article1 = NormalArticle("DataClasses",
"vibhu4agarwal",
"Python", 0)
article2 = NormalArticle("DataClasses",
"vibhu4agarwal",
"Python", 0)
Python3
print("DataClass Equal:", dClassArticle1 == dClassArticle2)
print("Normal Class Equal:", article1 == article2)
Output:
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.
Similar Reads
Introduction to Python GIS Geographic Information Systems (GIS) are powerful tools for managing, analyzing, and visualizing spatial data. Python, a versatile programming language, has emerged as a popular choice for GIS applications due to its extensive libraries and ease of use. This article provides an introduction to Pytho
4 min read
Introduction to Python for Absolute Beginners Are you a beginner planning to start your career in the competitive world of Programming? Looking resources for Python as an Absolute Beginner? You are at the perfect place. This Python for Beginners page revolves around Step by Step tutorial for learning Python Programming language from very basics
6 min read
Introduction to JustPy | A Web Framework based on Python JustPy is a web framework that leverages the power of Python to create web applications effortlessly. In this article, we'll explore JustPy, its features, and why it's gaining attention among developers. What is the JustPy Module of Python?The JustPy module of Python is a web framework like Django b
8 min read
The Ultimate Guide to Data Classes in Python 3.7 This article discusses data classes in Python 3.7 and provides an introductory guide for data classes in Python 3.7 and above. Data Class is a new concept introduced in Python 3.7 version. You can use data classes not only as a knowledge container but also to write boiler-plate code for you and sim
4 min read
DSA in Python - Online Course For Beginners We all must have learned in school that a program needs instructions to perform any dedicated task such as sequencing a number, sorting tables, etc. However, these tasks are required to store, retrieve, and perform to form a symmetric structure in the virtual system. In other words, a data structure
9 min read