I can use kwargs
when creating a dataclass object without issues:
@dataclass()
class Data:
name: str = 'Unnamed'
additional: int = None
things: int = None
dic = {'additional': 123, 'things': 456}
res = Data(name='Somename', **dic)
Output: Data(name='Somename', additional=123, things=456)
However, I'd like to unpack the kwargs
inside the data class when they're not part of calling arguments:
@dataclass()
class Data:
name: str = 'Unnamed'
additional: int = None
things: int = None
def __post_init__(self):
self(**dic) #unpack kwargs to fields
dic = {'additional': 123, 'things': 456}
res = Data(name='Somename')
A
dataclass
may explicitly define an__init__()
method.The documentation states:
Hence, you may define a standard
__init__()
method to processkwargs
.