Data Classes in Python | An Introduction by ganesh kavhar

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.

# 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__() 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.

article = GfgArticle() 

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.

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) 

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.





要查看或添加评论,请登录

ganesh kavhar的更多文章

  • Python NumPy for Data Science

    Python NumPy for Data Science

    NumPy Introduction NumPy stands for ‘Numerical Python.’ It is a package in Python to work with arrays.

  • Becoming a Better Programmer

    Becoming a Better Programmer

    A smart programmer is one who understands that his or her work is never truly done. It doesn't matter how much you…

  • Data Processing in Machine Learning

    Data Processing in Machine Learning

    ML | Understanding Data Processing Data Processing is a task of converting data from a given form to a much more usable…

  • Operator in C programing by ganesh kavhar

    Operator in C programing by ganesh kavhar

    An operator in a programming language is a symbol that tells the compiler or interpreter to perform a specific…

  • Numpy by ganesh kavhar

    Numpy by ganesh kavhar

    Python Numpy Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array…

  • Cython to Wrap Existing C Code

    Cython to Wrap Existing C Code

    What is Cython ? It is an optimizing static compiler for both the Python programming language and the extended Cython…

  • Python for Data Analysis by ganesh kavhar

    Python for Data Analysis by ganesh kavhar

    A friend recently asked this and I thought it might benefit others if published here. This is for someone new to Python…

  • Why learning C Programming is a must?

    Why learning C Programming is a must?

    C is a procedural programming language. It was initially developed by Dennis Ritchie between 1969 and 1973.

  • String Operation in Python

    String Operation in Python

    String: Strings in an array of bytes which represent Unicode characters in python. Python does not support character…

  • Data Types In Python

    Data Types In Python

    Today, in this article, we will learn about Python data types and their usage. This is a very important topic because…

社区洞察

其他会员也浏览了