1 回答
TA贡献1784条经验 获得超9个赞
实际上,任何用户定义的对象都可以(没有定义 a __slots__)。Python 用户定义的对象很乐意接受您希望分配给它们的任何属性。或者,您也可以只使用一个SimpleNamespace对象:
In [1]: from types import SimpleNamespace
In [2]: self = SimpleNamespace()
In [3]: self.vlist = {}
...: for i in self.vlist:
...: if i not in self.ulist:
...: self.ulist[i] = {}
...: self.ulist[i]['appears'] = []
...:
In [4]: self
Out[4]: namespace(vlist={})
至少有一个很好的开箱即用的印刷表示。
但同样,你总是可以这样做:
In [5]: class Foo: pass
In [6]: self = Foo()
In [7]: self.vlist = {}
...: for i in self.vlist:
...: if i not in self.ulist:
...: self.ulist[i] = {}
...: self.ulist[i]['appears'] = []
...:
In [8]: self
Out[8]: <__main__.Foo at 0x105fde048>
In [9]: self.vlist
Out[9]: {}
添加回答
举报