Data Classes in Python | Set 2 (decorator options)

Python Methods and Functions

In this post, we will discuss how to change the default constructor that the dataclass module actually does for us.

dataclass ()

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

By changing the values ​​of these parameters, we can change the behavior and operation of the default constructor created for our data classes.

init: This parameter specifies whether or not there should be a default constructor.

 True (default): There will be a default Constructor. False: There won't be a default Constructor. 

 TypeError: object () takes no parameters 

repr: This parameter specifies how the function will behave __ r epr __ () . The False value matches the hash value of the object's memory representation. True matches the entity's representation in the DataClass.

from dataclasses import dataclass

 

@ dataclass (init = False )

class GfgArticle ():

 

title: str

author: str

language: str

upvotes: int

 
# DataClass object

article = GfgArticle ( "DataClasses" ,

"vibhu4agarwal" ,

  " Python " , 0 )

from dataclasses import dataclass

 

@ dataclass ( repr = False )

class GfgArticle ():

 

title: str

author: str

  language: str

upvotes: int

  
# DataClass object

article = GfgArticle ( " DataClasses " ,

  " vibhu4agarwal " ,

  "Python" , 0 )

print (article)

Exit:

 __main __. GfgArticle object at 0x7f391b7ddbe0 

eq: This parameter is used used to specify the operation to be performed in a comparison when two data classes are compared for equality using the == or operators! = . eq accepts boolean values.

from dataclasses import dataclass

 

@ dataclass ( repr = False , eq = False )

class GfgArticle ( ):

 

title: str

author: str

language: str

upvotes: int

 
# Two DataClass objects

dClassArticle1 = GfgArticle ( "DataClasses" ,

"vibhu4agarwal" ,

"Python" , 0 )

  

dClassArticle2 = GfgArticle ( " DataClasses " ,

  "vibhu4agarwal" ,

" Python " , 0 )

 

equal = dClassArticle1 = = dClassArticle2

print ( 'Classes Equal:' , equal)

Output:

 __main __. GfgArticle object at 0x7ff501c63c18 __main __. GfgArticle object at 0x7ff501c63ba8 Classes Equal: False 

When eq = False , two objects They are compared using their hash based on its location in memory, like two ordinary objects. Since the two objects have different hash representations, their equality returns False.

order: The comparison between the two data classes is not only limited to equality, but also supports & gt; , & gt; = , & lt; and & lt; =, if for argument parameter is set to order = True ...
The comparison between objects is based on a comparison of their respective attributes, which is done one by one, starting with the first.

from dataclasses import dataclass 

 

@ dataclass (order = True )

class A ():

var1: int

var2: str

var3: float

 

obj1 = A ( 1 , "GeeksForGeeks" , 7.0 )

obj2 = A ( 2 , "GeeksForGeeks" , 7.0 )

obj3 = A ( 1 , " GfG " , 7.0 )

obj4 = A ( 1 , "GeeksForGeeks" , 8.0 )

 

print (obj1 & gt; obj2)

print (obj1 & lt; obj3)

print (obj1 & gt; = obj4)

Output:

 False True False 

frozen: this sets all variables in the DataClass as one-time initializable, which after initialization cannot be reassigned new meaning. C++ users can associate this with const, and Java users can associate this with last keyword.

from dataclasses import dataclass 

 

@ dataclass (frozen = True )

class GfgArticle ():

 

title: str

author: str

language: str

upvotes: int

  

dClassArticle = GfgArticle ( "DataClasses" ,

"vibhu4agarwal" ,

"Python" , 0 )

print (dClassArticle)

  

dClassArticle.upvotes = 100

print (dClassArticle)

GfgArticle (title = 'DataClasses', author = 'vibhu4agarwal', language = 'Python', upvotes = 0)
Traceback (most recent call last):
File “dClass.py”, line 16, in & lt; module & gt;
dClassArticle.upvotes = 100
File “& lt; string & gt;”, line 3, in __setattr__
dataclasses.FrozenInstanceError : cannot assign to field 'upvotes'

unsafe_hash: Typically, mutable objects in Python are Unhashable . This means that their hash cannot be generated using the

from dataclasses import dataclass 

 
@ dataclass

class GfgArticle ():

 

title: str

author: str

language: str

upvotes: int

 

dClassArticle = GfgArticle ( "DataClasses" ,

"vibhu4agarwal" ,

"Python" , 0 )

print (dClassArticle)

print ( hash (dClassArticle))

GfgArticle (title = 'DataClasses', author = 'vibhu4agarwal', language = 'Python', upvotes = 0)
Traceback (most recent call last):
File “dClass.py”, line 15, i n & lt; module & gt;
print (hash (dClassArticle))
TypeError: unhashable type: 'GfgArticle'

However, frozen = True sets the one-time initialization variables and hence makes the object immutable. This safely generates a hash for the DataClass object.

from dataclasses import dataclass 

  

@ dataclass (frozen = True )

class GfgArticle ():

 

title: str

author: str

language: str

upvotes: int

 

dClassArticle = GfgArticle ( "DataClasses" ,

"vibhu4agarwal" ,

"Python" , 0 )

print (dClassArticle)

print ( hash (dClassArticle))

Output:

GfgArticle (title = 'DataClasses', aut hor = 'vibhu4agarwal', language = 'Python',upvotes = 0)
9111021502060859577

unsafe_hash forces the DataClass, which is still mutable, to generate a hash.

This case is used, when we logically know that after initialization we are not going to change the values ​​of the Dataclass attributes. But it boils down to the question of whether can they be changed? or in other words, is DataClass not frozen or not? When using unsafe_hash , if The DataClass is not frozen, then the DataClass generates a unsafe hash, assuming the class is frozen, and then the programmer must use this very carefully.

from dataclasses import dataclass 

 

@ dataclass (unsafe_hash = True )

class GfgArticle ():

  

title: str

  author: str

language: str

upvotes: int

  

dClassArticle = GfgArticle ( "DataClasses" ,

"vibhu4agarwal" ,

"Python" , 0 )

print (dClassArticle)

print ( hash (dClassArticle))

Exit:

GfgArticle (title = 'DataClasses', author = 'vibhu4agarwal', language = 'Python', upvotes = 0)
8924105619418522237





Get Solution for free from DataCamp guru