Data Classes in Python | Set 5 (post-init)
Last Updated :
15 Apr, 2019
Prerequisite:
Data Classes in Python | Set 4
In this post, we will discuss how to modify values of some attributes during object creation without coding it in
__init__()
by using post-init processing.
__post_init__()
: This function when made, is called by in-built __init__() after initialization of all the attributes of DataClass. Basically, object creation of DataClass starts with
__init__()
(constructor-calling) and ends with
__post__init__()
(post-init processing).
Use -
This feature is very handy at times when certain attributes are dependent on the parameters passed in the
__init__()
but do not get their values directly from them. That is, they get their values after performing some operation on a subset of arguments received in the constructor.
Continuing the same example we've been seeing in this series of articles, suppose there is an attribute called
author_name
which gets its value from the profile handle to name mapping in the defined dictionary
name
.
So
author_name
is dependent on profile handle which
author
attribute receives, so using
__post_init__()
should be an ideal choice this case.
Python3
from dataclasses import dataclass, field
name = {'vibhu4agarwal': 'Vibhu Agarwal'}
@dataclass
class GfgArticle:
title : str
language: str
author: str
author_name: str = field(init = False)
upvotes: int = 0
def __post_init__(self):
self.author_name = name[self.author]
dClassObj = GfgArticle("DataClass", "Python3", "vibhu4agarwal")
print(dClassObj)
Output:
GfgArticle(title='DataClass', language='Python3', author='vibhu4agarwal', author_name='Vibhu Agarwal', upvotes=0)
Can default_factory
be an alternative?
No, because
default_factory
accepts zero argument function or callable, so it can't receive any arguments and return a value after performing some operations on them.
Similar Reads
Data Classes in Python | An Introduction 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
3 min read
Data Classes in Python | Set 2 (Decorator Parameters) Prerequisite: Data Classes in Python | Set 1 In this post, we will discuss how to modify the default constructor which dataclass module virtually makes for us. dataclass() decorator - @dataclasses.dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False) By changing t
4 min read
Using a Class with Input in Python In this article, we will see how to take input using class in Python. Using a Class with Input in Python It is to be noted that while using class in Python, the __init__() method is mandatory to be called for declaring the class data members, without which we cannot declare the instance variable (da
2 min read
The Ultimate Guide to Data Classes in Python 3.7 This article discusses data classes in Python 3.7 and provides an introductory guide for data classes in Python 3.7 and above. Data Class is a new concept introduced in Python 3.7 version. You can use data classes not only as a knowledge container but also to write boiler-plate code for you and sim
4 min read
Matplotlib.artist.Artist.set() in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The Artist class contains Abstract base class for objects that render into a FigureCanvas. All visible elements in a figure are subclasses of Artist. matplotlib.artist.Artist.set() method The set() meth
2 min read