📜  python嵌套对象到dict - 任何代码示例

📅  最后修改于: 2022-03-11 14:59:22.702000             🧑  作者: Mango

代码示例1
def my_dict(obj):
    if not  hasattr(obj,"__dict__"):
        return obj
    result = {}
    for key, val in obj.__dict__.items():
        if key.startswith("_"):
            continue
        element = []
        if isinstance(val, list):
            for item in val:
                element.append(my_dict(item))
        else:
            element = my_dict(val)
        result[key] = element
    return result