Please enable JavaScript.
Coggle requires JavaScript to display documents.
Dataclasses - Coggle Diagram
The DataClass module provides a handy way to make classes less wordy.
DataClasses are like normal classes in Python, but they have some basic functions like instantiation, comparing, and printing the classes already implemented.
dataclasses.Field()
The field() objects describe each defined field.Syntax: dataclasses.field(*, default=MISSING, default_factory=MISSING, repr=True, hash=None, init=True, compare=True, metadata=None)Parameters:
- default
- default_factory
- init
- repr
- hash
- compare
- metadata
-
default_factory
This field accepts a function and returns the initial value of the field, it must be a zero-argument.
def get_emp_id():
id = 2345
return id
emp_id: str = field(default_factory=get_emp_id)
-
repr
If true (the default), this field is included in the string returned by the generated repr() method.
hash
If true, this field is included in the generated hash() method. If false then it will not consider these field.
compare
If true (the default), this field is included in the generated equality and comparison methods (eq(), gt().
-
-
_._post_init__()
While creating object post_init() method call automatically. init() code will call a method named post_init().
from dataclasses import dataclassdataclassclass employee:
(# Attributes Declaration)
(# using Type Hints)
name: str
emp_id: str
age: int
city: str