attrs.asdict() 中缺少列表类型的双下划线?

Missing dunders for list type in attrs.asdict()?

我试图使用 Python attrs 装饰器创建 list() class 的嵌套,并注意到 attrs.asdict() 在某些情况下不起作用子变量的水平。这样:

我的工作示例是:

import attr


@attr.s
class MyElement(object):
    element = attr.ib(default="mydefault", type=str)

@attr.s(slots=True)
class MyListOfElements(object):
    one_element = attr.ib(default=attr.Factory(MyElement))
    list_of_elements = attr.ib(default=attr.Factory(list), type=list)
    def add(self, le):
        self.list_of_elements.append(le)

@attr.s(slots=True)
class MyListOfList(object):
    version = attr.ib(default=None, type=str)
    list_of_list_of_elements = attr.ib(default=attr.Factory(list))  # Where's the dunder for this list?

e1 = MyElement("1.1.1.1")
e1_1 = MyElement("11.11.11.11")
le1 = MyListOfElements()
le1.add(e1)
le1.add(e1_1)
print("le1:", le1)

e2 = MyElement("2.2.2.2")
le2 = MyListOfElements(e2)
le2.list_of_elements.append(e2)
print("le2:", le2)

mlle = MyListOfList()
print("mlle:", mlle)
mlle.list_of_list_of_elements.append(le1)
mlle.list_of_list_of_elements.append(le2)
print("mlle:", mlle)

现在开始使用 attr.asdict() 功能...

# attr.Dict
print("asdict(mlle):", attr.asdict(mlle))
print("asdict(mlle.lle):", attr.asdict(mlle.list_of_list_of_elements))  # FAILS
print("asdict(mlle.lle[0]):", attr.asdict(mlle.list_of_list_of_elements[0]))

我很确定我做错了一些简单的事情,因为我认为我用 attrs.

修饰了所有嵌套变量

我更困惑的是 attr.asdict() 在每个带点的子变量上的使用并不顺利,因此 mllemlle.list_of_list_of_elements[0] 有效,但 mlle.list_of_list_of_elements 在之间。

attr.asdict 采用 attr.s 修饰的 class 的实例。 mllemlle.list_of_list_of_elements[0] 都是 attr.s 修饰的 class 的实例。 mlle.list_of_list_of_elements 不是。