def dataclass(cls=None, /, *, init=True, repr=True, eq=True, order=False,unsafe_hash=False, frozen=False):
1. init
2. repr
3. eq
4. order
5.unsafe_hash
6. frozen
1. init-This is the first parameter in the python data class and this parameter is set to True by
default. This creates the initialization function for the class with the class attributes.
Example-
When init is set to false-
from dataclasses import dataclass
@dataclass(init=False)
class Student():
name: str
clss: int
Roll_no: int
# A DataClass object
student = Student('ABC', 10, 17)
print(student)
OUTPUT-
In the above code, we firstly used the data class decorator and declared the class attributes with type hint. Here we took the init parameter as false. Therefore when there is no init function in a class, that means there are no class attributes and thus the Student class does not take any arguments. If we set this init parameter to default i.e. true simply the code is converted to a simple class with the init function defined.
2. repr-This parameter provides the representation of the class it specifies how the __repr__() function will behave. If set to False, the class object will return its memory address.If set to True it corresponds
to the DataClass representation of the object.
Example-
When repr is set to default i.e true-
from dataclasses import dataclass
@dataclass()
class Student():
name: str
clss: int
roll_no: int
# A DataClass object
student = Student('ABC', 11, 20)
print(student)
Output-
3. eq-This parameter is basically a comparison. It is for equality check between objects of the class. eq takes in boolean values. It can be represented by == or =!.
Example-
When eq is set to true-
from dataclasses import dataclass
@dataclass(eq=True)
class Student():
name: str
clss: int
roll_no: int
# A Data class object
student1 = Student('ABC', 10, 17)
student2 = Student('ABC', 10, 17)
print(student1 == student2)
Output-
4. order- This parameter implements the relational operators like >,>=,< <= on the objects of the class so, Comparison between two DataClasses are not only restricted only to equality but also supports relational operators.
Example-
When the order is set to true-
from dataclasses import dataclass
@dataclass(order=True)
class Student():
name: str
clss: int
roll_no: int
# A data class object
student1 = Student('ABC', 10, 17)
student2 = Student('Harry', 10, 12)
print(student1 >student2)
print(student1 <student2)
Output-
When the order parameter is set to False(Default), we will not able to compare the two objects.
5. unsafe_hash-
This parameter in python is unhashable which means their hash cannot be generated using the hash() function. In python, the hash is calculated only for immutable objects, and the object of a class is mutable, so the hash cant be calculated. But, when the unsafe_hash parameter in the data class is set to True, the hash can be generated for the object.
Example-
When unsafe_hash is set to true-
from dataclasses import dataclass
@dataclass(unsafe_hash=True)
class Student():
name: str
clss: int
roll_no: int
# A Data class object
student = Student('ABC', 12, 14)
print(hash(student))
Output-
6.frozen This parameter sets all the variables in the DataClass as one-time initializable and once the data class is initialized, it cannot be reassigned to a new value. By default, the object of a class is mutable, if you want to make your class object immutable, set the Frozen parameter asTrue.
Example-
When frozen is set to true-
from dataclasses import dataclass
@dataclass(frozen=True)
class Student():
name: str
clss: int
roll_no: int
student = Student('ABC', 10, 17)
student.name = 'Harry'
print(student)
Output-
This makes the class object immutable.
Name | Views | Likes |
---|---|---|
Find Changed Elements using difflib in python | 188 | 0 |
Optimizing Data Classes in Python | 187 | 1 |
Immutable Data Classes in Python | 242 | 0 |
Post-Init Processing In Python Data Class | 311 | 2 |
Inheritance in Python DataClass | 272 | 2 |
Data Class Fields in Python | 242 | 2 |
Data Class Parameters in Python | 281 | 2 |
Introduction to Data Classes in Python | 366 | 2 |
Comments