嵌套数据类听写

nested dataclasses to dict

我有很多使用 post_init

嵌套的数据类
from dataclasses import dataclass
from typing import List
from typing import Optional
from typing import Union


@dataclass
class MyClass:
    signed_key: str
    signature: str


@dataclass
class Message:
    signature: str
    my_class: Union[MyClass, dict]
    protocol_version: str
    signed_message: str

    def __post_init__(self):
        self.my_class = MyClass(**self.my_class)

它工作得很好,但问题是如果我想像这样将 Message 转换为 dict:

#create a message object before
print(message.__dict__)

我得到的输出:

{'signature': 'test', 'my_class': Myclass(signed_key='test', signature='test'), 'protocol_version': 'test', 'signed_message': 'test'}

我想要的(嵌套字典):

{'signature': 'test', 'my_class': {'signed_key': 'test', 'signature': 'test'}, 'protocol_version': 'test', 'signed_message': 'test'}

我本可以使用像 attrs 或 pydantic 这样的库,但这里的目标是只使用纯 python

您可以使用 dataclasses.asdict(请注意,这是一个模块级函数,未绑定到任何数据类实例),它正是为此目的而设计的。

dataclasses.asdict(instance, *, dict_factory=dict)

Converts the dataclass instance to a dict (by using the factory function dict_factory). Each dataclass is converted to a dict of its fields, as name: value pairs. dataclasses, dicts, lists, and tuples are recursed into.

示例,

>>> from dataclasses import dataclass, asdict
>>>
>>> @dataclass
... class Message:
...     message: str
...
>>>
>>> @dataclass
... class Chat:
...     messages: list[Message]
...
>>> chat = Chat([Message('Hi'), Message("Hello")])
>>> chat.__dict__
{'messages': [Message(message='Hi'), Message(message='Hello')]}
>>> asdict(chat)
{'messages': [{'message': 'Hi'}, {'message': 'Hello'}]}