Python __dict__属性详解
我们都知道 Python 一切皆对象,那么 Python 究竟是怎么管理对象的呢?
1、无处不在的 __dict__
首先看一下类的__dict__ 属性和类对象的__dict__ 属性
# -*- coding: utf-8 -*-class A(object):
"""
Class A.
"""a </span>=<span style="color: rgba(0, 0, 0, 1)"> 0 b </span>= 1 <span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self): self.a </span>= 2<span style="color: rgba(0, 0, 0, 1)"> self.b </span>= 3 <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> test(self): </span><span style="color: rgba(0, 0, 255, 1)">print</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a normal func.</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> @staticmethod </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> static_test(self): </span><span style="color: rgba(0, 0, 255, 1)">print</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a static func.</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> @classmethod </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> class_test(self): </span><span style="color: rgba(0, 0, 255, 1)">print</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a calss func.</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
obj = A()
print A.dict
print obj.dict
运行结果如下:
{'a': 0, '__module__': '__main__', 'b': 1, 'class_test': <classmethod object at 0x00000000021882E8>, '__dict__': <attribute '__dict__' of 'A' objects>, '__init__': <function __init__ at 0x00000000023A5BA8>, 'test': <function test at 0x00000000023A5C18>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': '\n Class A.\n ', 'static_test': <staticmethod object at 0x00000000021881C8>} {'a': 2, 'b': 3}
由此可见, 类的静态函数、类函数、普通函数、全局变量以及一些内置的属性都是放在类 __dict__里的
对象的 __dict__中存储了一些 self.xxx 的一些东西
2、Python 里什么没有 __dict__ 属性
虽然说一切皆对象,但对象也有不同,就好比不是每个人的女朋友都是一个人一样,一些内置的数据类型是没有 __dict__ 属性的,如下:
num = 3 ll = [] dd = {} print num.__dict__ print ll.__dict__ print dd.__dict__
当我们运行这样的代码时,解释器就会告诉我们
Traceback (most recent call last): File "f:\python\test.py", line 54, in <module> print num.__dict__ AttributeError: 'int' object has no attribute '__dict__'Traceback (most recent call last):
File "f:\python\test.py", line 55, in <module>
print ll.dict
AttributeError: 'list' object has no attribute 'dict'Traceback (most recent call last):
File "f:\python\test.py", line 56, in <module>
print dd.dict
AttributeError: 'dict' object has no attribute 'dict'
int, list, dict 等这些常用的数据类型是没有 __dict__ 属性的,其实这是可预料的,就算给了它们 dict 属性也没啥用,毕竟它们只是用来做数据容器的。
3、发生继承时候的 __dict__ 属性
子类有自己的 __dict__, 父类也有自己的 __dict__, 子类的全局变量和函数放在子类的 dict 中,父类的放在父类 dict 中。
# -*- coding: utf-8 -*-class Parent(object):
a = 0
b = 1<span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self): self.a </span>= 2<span style="color: rgba(0, 0, 0, 1)"> self.b </span>= 3 <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> p_test(self): </span><span style="color: rgba(0, 0, 255, 1)">pass</span>
class Child(Parent):
a = 4
b = 5<span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self): super(Child, self).</span><span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">() # self.a </span>= 6<span style="color: rgba(0, 0, 0, 1)"> # self.b </span>= 7 <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> c_test(self): </span><span style="color: rgba(0, 0, 255, 1)">pass</span> <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> p_test(self): </span><span style="color: rgba(0, 0, 255, 1)">pass</span><span style="color: rgba(0, 0, 0, 1)">
p = Parent()
c = Child()
print Parent.dict
print Child.dict
print p.dict
print c.dict
运行上面的代码,结果入下:
{'a': 0, '__module__': '__main__', 'b': 1, '__dict__': <attribute '__dict__' of 'Parent' objects>, 'p_test': <function p_test at 0x0000000002325BA8>, '__weakref__': <attribute '__weakref__' of 'Parent' objects>, '__doc__': None, '__init__': <function __init__ at 0x0000000002325B38>} {'a': 4, 'c_test': <function c_test at 0x0000000002325C88>, '__module__': '__main__', 'b': 5, 'p_test': <function p_test at 0x0000000002325CF8>, '__doc__': None, '__init__': <function __init__ at 0x0000000002325C18>} {'a': 2, 'b': 3} {'a': 2, 'b': 3}
1)上段输出结果中,用红色字体标出的是类变量和函数,由结果可知,每个类的类变量、函数名都放在自己的 __dict__ 中
2) 再来看一下实力变量的 __dict__ 中,由蓝色字体标识,父类和子类对象的 __dict__ 是公用的
总结:
1) 内置的数据类型没有 __dict__ 属性
2) 每个类有自己的 __dict__ 属性,就算存着继承关系,父类的 __dict__ 并不会影响子类的 __dict__
3) 对象也有自己的 __dict__ 属性, 存储 self.xxx 信息,父子类对象公用 __dict__
勘误:
多谢几位同学的 @TianYuanSX, @企鹅 lin, @halo1234, 父子类对象不是公用 dict,验证方法 3 楼已经说了;同时也可以用 id(p.__dict__) id(c.__dict__) 验证,两个确实不同。