×

Data Class in Python

Introduction

In Python 3.7, the dataclass modules are introduced as a practical tool for creating organized classes designed specifically for data storage. These classes contain specific attributes and capabilities to deal with data and its representations in particular.

DataClasses in Python3.6, a popular language

Even though the module was first introduced in Python3.7, it is also compatible with Python3.6 when the dataclasses library is installed.

pip install dataclasses

Decorators and classes are used to implement the DataClasses. In order to describe the data type for variables in Python, attributes are defined using Type Hints.

Code

# fundamental Data Class
# dataclass module import
from dataclasses import dataclass


@dataclass
class JTP_Article():
	"""a class for storing article contents"""


	# Declaring Attributes Using Type Hints




	title: str
	author: str
	language: str
	upvotes: int


# object of the data class
art = JTP_Article("Data Class", "Annu Chaudhary", "Python", 0)
print(art)

Output:

JTP_Article(title='Data Classe', author='Annu Chaudhary', language='Python',upvotes=0)

The two notable points in the code above.

  • The class received values and allocated them to the proper variables without a __init__() constructor.
  • Without any specific function being implemented to do this, the output of a printing object is a clear representation of the data it contains. That indicates that the __repr__() function has been altered.

A built-in __init__() constructor is made available by the dataclass to classes that manage object and data creation on their behalf.

Code:

# fundamental Data Class


# dataclass module import
from dataclasses import dataclass


@dataclass
class JTP_Article():
	"""a class for storing article contents"""


	# Declaring Attributes Using Type Hints




	title: str
	author: str
	language: str
	upvotes: int


# object of the data class
art = JTP_Article ()
print(art)

Output:

Traceback (most recent call last):
  File "d:\Programming\Python\test.py", line 20, in <module>
    art = JTP_Article ()
TypeError: JTP_Article.__init__() missing 4 required positional arguments: 'title', 'author', 'language', and 'upvotes'

Equality of Data Classes

Since the classes hold data, it is frequently necessary to check two objects to see if they share the same data when using dataclasses. The == operator is used to achieve this.

The code for a class that can store an article but without dataclass decorator is provided below.

Code:

# fundamental Data Class


# dataclass module import
from dataclasses import dataclass


@dataclass
class JTP_Article ():
	"""a class for storing article contents"""


	# Declaring Attributes Using Type Hints




	title: str
	author: str
	language: str
	upvotes: int




class Normal_Article():
	"""a class for storing article contents"""


	# Equivalent Constructor
	def __init_(self, title, author, language, upvotes):
		self.title = title
		self.author = author
		self.language = language
		self.upvotes = upvotes


# Two objects of dataclass
Article_1 = JTP_Article("DataClass", "Annu_Chaudhary", "Python", 0)
Article_2 = JTP_Article("DataClass", "Annu_Chaudhary", "Python", 0)


# Two objects of a normal class
article_1 = Normal_Article("DataClass", "Annu_Chaudhary", "Python", 0)
article_2 = Normal_Article("DataClass", "Annu_Chaudhary", "Python", 0)


print ("Data Class Equal:", Article_1 == Article_2)
print ("Normal Class Equal:", article_1 == article_2)

Output:

Data Class Equal: True
Normal Class Equal: False

When two objects are equal in Python, the == operator looks for the same memory location. The outcome for equality is False because two objects require distinct memory locations when they are created. The equality of the data included in each DataClass object is checked. When two DataClass objects with identical data are checked for equality, the output is True in this case.

dataclass() decorator –

@dataclasses.dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False)

We can alter the functionality and operation of the default constructor created for our DataClasses by altering the values of these parameters.

init: This option indicates whether or not a default constructor should be used.

True (default): A default Constructor shall exist.

False: A default Constructor shall not exist.

Code:

from dataclasses import dataclass


@dataclass(init = False)
class JTP_Article():


	title: str
	author: str
	language: str
	upvotes: int


# An object of the dataclass
art = JTP_Article("Data Class", "Annu_Chaudhary", "Python", 0)

Output:

Traceback (most recent call last):
  File "d:\Programming\Python\test.py", line 12, in <module>
    art = JTP_Article("Data Class", "Annu_Chaudhary", "Python", 0)
TypeError: JTP_Article() takes no arguments

repr: The __repr__() function's behavior is specified by this parameter. The hash value representing the object in memory corresponds to the false value. The object's DataClass representation corresponds to the true value.

Code:

from dataclasses import dataclass


@dataclass (repr = False)
class JTP_Article ():


	title: str
	author: str
	language: str
	upvotes: int


# An object of the data class
art = JTP_Article ("DataClasses", "Annu_Chaudhary", "Python", 0)
print (art)

Output:

<__main__.JTP_Article object at 0x00000216E1D675E0>

eq: When two DataClasses are checked for equality with the == or!= operators, this parameter will be used to identify the comparison operation that was carried out. Boolean values are passed to eq.

Code:

from dataclasses import dataclass


@dataclass (repr = False, eq = False)
class JTP_Article():


	title: str
	author: str
	language: str
	upvotes: int


# Two objects of data class
Article_1 = JTP_Article ("DataClasses", "vibhu4agarwal", "Python", 0)


Article_2 = JTP_Article ("DataClasses", "vibhu4agarwal", "Python", 0)


equal = Article_1 == Article_2
print ('Classes Equal:', equal)

Output:

Classes Equal: False

When eq is False, two objects are compared using their hashes based on where they are stored in memory, much like two regular objects. The equality of the two items returns False because they have distinct hash representations.

order: When order=True is specified in the input parameter, comparison among two DataClasses is not limited to equality but also supports the >, >=, and = operators.

Based on a comparison of the corresponding qualities of each object, starting with the first one, comparisons between the objects are made.

Code:

from dataclasses import dataclass
	
@dataclass(order = True)
class Fun ():
	var_1: int
	var_2: str
	var_3: float


obj_1 = Fun (1, "javaTpoint", 7.0)
obj_2 = Fun (2, "javaTpoint", 7.0)
obj_3 = Fun (1, "JTP", 7.0)
obj_4 = Fun (1, "javaTpoint", 8.0)


print (obj_1 > obj_2)
print (obj_1 < obj_3)
print (obj_1 >= obj_4)

Output:

False
False
False

Frozen: This designates all DataClass variables as one-time initializable, meaning that once initialized, they cannot be given a new value. This is comparable to the const keyword in C++ and the final keyword in Java.

Code:

from dataclasses import dataclass
	
@dataclass (frozen = True)
class JTP_Article():
	
	title: str
	author: str
	language: str
	upvotes: int


Article = JTP_Article("DataClass", "Annu_chaudhary", "Python", 0)
print (Article)


Article.upvotes = 100
print (Article)

Output:

JTP_Article(title='DataClass', author='Annu_chaudhary', language='Python', upvotes=0)
Traceback (most recent call last):
  File "d:\Programming\Python\test.py", line 14, in <module>
    Article.upvotes = 100
  File "<string>", line 4, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field 'upvotes'

unsafe_hash: In Python, mutable objects are often Unhashable. This means that their hash cannot be produced using Python's hash() method.

DataClass objects are mutable because every class object, including them, has the ability to change its values. They should therefore be unable to generate any hash values.

Code:

from dataclasses import dataclass
	
@dataclass
class JTP_Article ():
	
	title: str
	author: str
	language: str
	upvotes: int


Article = JTP_Article ("DataClass", "Annu_Chaudhary", "Python", 0)
print (Article)
print (hash (Article))

Output:

JTP_Article(title='DataClass', author='Annu_Chaudhary', language='Python', upvotes=0)
Traceback (most recent call last):
  File "d:\Programming\Python\test.py", line 13, in <module>
    print (hash (Article))
TypeError: unhashable type: 'JTP_Article'

However, frozen=True makes the object inalterable by initializing the variables only once. This creates a hash for DataClass object in a secure manner.

Code:

from dataclasses import dataclass
	
@dataclass (frozen = True)
class JTP_Article ():
	
	title: str
	author: str
	language: str
	upvotes: int


Article = JTP_Article ("DataClass", "Annu_Chaudhary", "Python", 0)
print (Article)
print (hash (Article))

Output:

JTP_Article(title='DataClasses', author='Annu_Chaudhary', language='Python', upvotes=0)
6601233036894739447

A DataClass that is still modifiable must generate a hash under the control of unsafe_hash.

When we logically know that we won't change the values of the Dataclass properties after initialization, we utilize this case. But ultimately, the question is: Can they be altered? alternatively, Is the DataClass frozen or not? If the DataClass is not frozen when using unsafe_hash, then DataClass produces an unsafe hash, presuming that the class is frozen, and the programmer must then use this very carefully moving forward.

Code:

from dataclasses import dataclass
	
@dataclass (unsafe_hash = True)
class JTP_Article ():
	
	title: str
	author: str
	language: str
	upvotes: int


Article = JTP_Article ("DataClass",	"Annu_Chaudhary", "Python", 0)
print (Article)
print (hash (Article))

Output:

JTP_Article(title='DataClasses', author='Annu_Chaudhary', language='Python', upvotes=0)
6601233036894739447

Related Topics

Pltpcolor in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Django vs NodeJS

Django and NodeJS are open-source frameworks used to develop web applications. Both Django and NodeJS are cross-platform and robust technology used for developing versatile web applications and mobile applications. Django Django is...

2 minutes read.

Python Set discard() method

Python Set discard() method The set.discard() method in Python removes a specified element from the set (if present). Syntax set.discard(value) Parameter value- This parameter represents the element to be removed from the set. Return None Example 1 # Python program explaining # the...

1 minute read.

Poolmanager in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python md5_file() function

Python md5_file() function The md5_file() function in PHP calculates the md5 hash of a given file. Syntax md5_file ( string $filename [, bool $raw_output ] )  Parameter filename(required)- This parameter signifies the file to be calculated. raw_output(optional)- It takes a boolean value that specifies hex or binary...

1 minute read.

Isodate Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

Python List index() method

Python List index() method The list.index () method in Python returns the position at the first occurrence of the specified value. Syntax list.index(x[, start[, end]]) Parameter element – This parameter represents the element whose lowest index will be returned. start (Optional)...

2 minutes read.

Python Switch Case

What is a Switch Case Statement? In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once...

3 minutes read.

Conditional Statements in python

In this article, we will learn about conditional statements and how to apply conditional statements in Python. A decision-making statement is another name for a conditional statement. Decision-making is an important...

5 minutes read.

AX Contour in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes read.

OS Module in Python

What is a module? The Modules are the kind of files that contains python statements and definitions. The module is known by the name of the file followed by the suffix...

8 minutes read.

Python vs PHP

Python Python is a high-level, object-oriented, interpreted language that is used to create independent program and algorithms for a variety of applications. It has a large library support base. In 1990,...

3 minutes read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

Writing to Excel using Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Front end in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

4 minutes read.

Python exe() function

Python exe() function The exec() function in Python executes the specified Python code and accepts large blocks of code. It supports dynamic execution of Python code where the object must be either a string...

1 minute read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

How to plot multiple linear regression in Python

This article lets us look into linear regression in Python and how we can plot multiple linear regressions in it. Linear regression One of the simplest and most widely used Machine Learning...

4 minutes read.