代码在前,
#Python 2.7
>>>class A(object):
pass
>>>a1 = A()
>>>a2 = A()
>>>A.__dict__
dict_proxy({'__dict__': <attribute '__dict__' of 'A' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None})
问题
1.什么是dict_proxy
为什么要使用它?
2。 A.__dict__
包含一个属性 -- '__dict': <attribute '__dict__' of 'A' objects>
.这是什么?是a1
吗和 a2
?但是 A
的对象有自己的__dict__
,不是吗?
最佳答案
对于您的第一个问题,我引用 Fredrik Lundh 的话:http://www.velocityreviews.com/forums/t359039-dictproxy-what-is-this.html :
a CPython implementation detail, used to protect an internal data structure used
by new-style objects from unexpected modifications.
关于python - Python 类的 `__dict__`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10498074/