python - Python 类的 `__dict__`

标签 python class

代码在前,

#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/

相关文章:

python - Matplotlib 添加基于现有颜色系列的图例

c++ - 使用bloodshed dev-c++创建泛型类调用的头文件是什么?

c++ - 指针类变量从 muncion 返回 NULL

c++ - C++中通过继承类重命名类成员

javascript - ES6 - 使用 import 语句在类上声明原型(prototype)方法

c# - 为什么静态类不能有非静态方法和变量?

python - 如何将包含重复值的列表转换为以列表为值的字典?

python - 如何用Python实时绘制每秒15000点连续读取的数据?

python - 从 python 中的集合列表中删除大括号

python - 枚举非确定性列表的所有可能性