Python __ dict __属性详解
我们都知道Python一切皆对象,那么Python究竟是怎么管理对象的呢?
本文转载自https://www.cnblogs.com/alvin2010/p/9102344.html
1、无处不在的__ dict __
首先看一下类的__ dict __ 属性和类对象 的__ dict __ 属性
>>> class A(object):
... a = 0
... b = 1
... def __init__(self):
... self.a = 2
... self.b = 3
... def test(self):
... print("a normal func.")
...
... @staticmethod
... def static_test(self):
... print("a static func.")
...
... @classmethod
... def class_test(self):
... print("a class func.")
...
>>> obj = A()
>>> print(A.__dict__)
{'__module__': '__main__', 'a': 0, 'b': 1, '__init__': <function A.__init__ at 0x10b5f61e0>, 'test': <function A.test at 0x10b5f6268>, 'static_test': <staticmethod object at 0x10b5f16d8>, 'class_test': <classmethod object at 0x10b5f1748>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
>>>
>>> print(obj.__dict__)
{'a': 2, 'b': 3}
由此可见,类的静态函数、类函数、普通函数、全局变量以及一些内置的属性都是放在类 __ dict __ 里的;
对象的 __ dict __ 中存储了一些self.xxx的一些东西;
###2、Python里什么没有__ dict __属性?
虽然说一切皆对象,但对象也有不同,一些内置的数据类型是没有 __ dict __ 属性的,如下:
>>> num = 3
>>> ll = []
>>> dd = {}
>>> print(num.__dict__)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute '__dict__'
>>> print(ll.__dict__)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute '__dict__'
>>> print(dd.__dict__)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute '__dict__'
int,list,dict等这些常用的数据类型是没有 __ dict __ 属性的,其实这是可预料的,就算给了它们dict属性也没啥用,毕竟它们只是用来做数据容器的。
3、发生继承时候的__ dict __() 属性
子类有自己的 __ dict __ ,父类也有自己的 __ dict __ ,子类的全局变量和函数放在子类的 dict 中,父类的放在父类 dict 中。
>>> class Parent(object):
... a = 0
... b = 1
...
... def __init__(self):
... self.a = 2
... self.b = 3
... def p_test(self):
... pass
...
>>> class Child(Parent):
... a = 4
... b = 5
...
... def __init__(self):
... super(Child, self).__init__()
... # self.a = 6
... # self.b = 7
... def c_test(self):
... pass
... def p_test(self):
... pass
...
>>> p = Parent()
>>> c = Child()
>>>
>>> print(Parent.__dict__)
{'__module__': '__main__', 'a': 0, 'b': 1, '__init__': <function Parent.__init__ at 0x10b5f6488>, 'p_test': <function Parent.p_test at 0x10b5f6510>, '__dict__': <attribute '__dict__' of 'Parent' objects>, '__weakref__': <attribute '__weakref__' of 'Parent' objects>, '__doc__': None}
>>>
>>> print(Child.__dict__)
{'__module__': '__main__', 'a': 4, 'b': 5, '__init__': <function Child.__init__ at 0x10b5f6598>, 'c_test': <function Child.c_test at 0x10b5f6620>, 'p_test': <function Child.p_test at 0x10b5f66a8>, '__doc__': None}
>>>
>>> print(p.__dict__)
{'a': 2, 'b': 3}
>>>
>>> print(c.__dict__)
{'a': 2, 'b': 3}
- 上段输出结果中,用红色字体标出的是类变量和函数,由结果可知,每个类的类变量、函数名都放在自己的 __ dict __ 中;
- 再来看下实例变量的 __ dict __ 中,父类和子类对象的 __ dict __ 是公用的。
总结:
- 内置的数据类型没有 __ dict __ 属性;
- 每个类有自己的 __ dict __ 属性,就算存着继承关系,父类的 __ dict __ 并不会影响子类的 __ dict __;
- 对象也有自己的 __ dict __ 属性,存储self.xxx信息,父子类对象公用 __ dict __ 。
本文转载自https://www.cnblogs.com/alvin2010/p/9102344.html