In this article, we will discuss the post-init processing function in the python data class.
We can modify values of some attributes during object creation without coding it in __init__() by using post-init processing.
The post-init function is a Python in-built function that takes advantage of the following properties: field init and variable. To get the init parameter set, you need to implement the field and pass its false value.
Let us understand with an example
We will find out the average marks of the student using post-init processing.
from dataclasses import dataclass, field
avg_marks: float = field(init=False)
self.avg_marks = sum(self.marks) / len(self.marks)
student = Student('ABC', 12, 19, [90, 92, 96])
You will need to implement the init parameter as false for the variable that you want to set in the following function. When the field init parameter is set to false for a variable we don't need to provide value for it while the object creation.
the avg_marks is set by adding all the marks and dividing them by the total length of the list in the __post_init__ function in python Data Class.