Dataclasses example
Fields
In this post we will discuss how to modify certain properties of the attributes of DataClass object, without explicitly writing code for it using field function.
field() function – dataclasses.field(*, default=MISSING, default_factory=MISSING, repr=True, hash=None, init=True, compare=True, metadata=None)
The above code puts one of the Python3, Java or CPP as default value for language while DataClass object creation. The init, repr and hash parameters are similar to that in the dataclass function as discussed in previous article. compare parameter can be related to order as that in dataclass function. The difference is being in their ability to be applicable only to a particular attribute, not to all the attributes in the DataClass under the decorator.
init : If true (the default), this field is included as a parameter to the generated init() method. A way to set default value should be provided when init is set to False.
repr : If true (the default), this field is included in the string returned by the generated repr() method.
compare : If true (the default), this field is included in the generated equality and comparison methods (eq(), gt(), et al.)
hash : This can be a bool or None. If true, this field is included in the generated hash() method. If None (the default), use the value of compare: this would normally be the expected behavior.
A field should be considered in the hash if it’s used for comparisons. Setting this value to anything other than None is discouraged.
metadata : This is usually a dictionary, the key-value pair indicating various information and it’s data.
In the script, it’s value can be accessed by querying dataclass_fields variable of the object.
Inheritance
Few points from above code:
Article is subclassed by GfgArticle
Both SuperClass and SubClass are DataClasses – although super-class or sub-class being a normal class is also possible. When a DataClass inherits a normal class, the init() from the super-class is overidden in sub-class.
author
in GfgArticle overrides the same in Article – As the basic concept of inheritance, the value for its assignment is first looked in the sub-class and followed up the tree in super-class.
Behaviour of init() of GfgArticle: If init() is not explicitly provided, the default init() expects attributes of super-class (Article) followed by attributes of sub-class as parameters.
If init() is explicitly provided, it should somehow initialize all it’s own attributes as well as those in the super-class (Article).
Post-init
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 postinit__() (post-init processing).
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.
interconversion to and from other datatypes
we will discuss how to get values of a DataClass object into a dictionary or tuple pairs and how to create a DataClass in a different way – from values, instead of defining it directly.
asdict() function –
:dataclasses.asdict(instance, *, dict_factory=dict)
One can simply obtain an attribute to value pair mappings in form of a dictionary by using this function, passing the DataClass object to the instance parameter of the function.
astuple() function –
:dataclasses.astuple(instance, *, tuple_factory=tuple)
Similar to asdict, one can simply obtain ordered values of dataclass attributes in the form of a tuple using this function by passing the DataClass object to the instance parameter of the function.
make_dataclass()
is_dataclass()
is_dataclass()
Last updated