Table Of Contents
Part 1: Introduction
dataclasses is a package from Python, and this package is not available until Python 3.7.
It is used to represent a certain data types: int, bool, float, str, Enums, Containers(List and Dict)
Part 2: A Data Class Without Initialization
Normally we need define
__init__ when we define a class, however,
dataclass does not need.
val:int=10variable, its type, and its initialization value
- class object variable is initialized according to its definition sequence, e.g.,
If we still need do some initialization,then
__post_init_ _ function.
Part 3: Frozen Variable
@dataclass(frozen = True)
val: int = 0
In this class a variable defined by
Number cannot be changed
Part 4: A Data Class with Initialization
1. Initialization with
default_factoryargument which can be used to initialize the field if a value is not passed at the time of creation of the object.
default_factoryshould be a callable( generally a function ) which accepts no arguments.
2. Select class members for comparison /represent ion/omitting initialization
field(compare = False)
Please check Python Dataclasses demonstration for codes illumination.