【发布时间】:2021-07-02 11:40:04
【问题描述】:
根据Python language documentation:
__slots__允许我们显式声明数据成员(如属性)并拒绝创建__dict__和__weakref__(除非在__slots__中显式声明或在父级中可用。)
所以我想知道这门课是否
>>> class A: __slots__ = ('__dict__', '__weakref__')
...
>>> del A.__slots__
>>> vars(A)
mappingproxy({
'__module__': '__main__',
'__dict__': <attribute '__dict__' of 'A' objects>,
'__weakref__': <attribute '__weakref__' of 'A' objects>,
'__doc__': None
})
相当于这个:
>>> class A: pass
...
>>> vars(A)
mappingproxy({
'__module__': '__main__',
'__dict__': <attribute '__dict__' of 'A' objects>,
'__weakref__': <attribute '__weakref__' of 'A' objects>,
'__doc__': None
})
【问题讨论】:
标签: python class instance python-internals slots