我們都知道Python一切皆對象,那么Python究竟是怎么管理對象的呢?
1、無處不在的__dict__
首先看一下類的__dict__屬性和類對象的__dict__屬性
# -*- coding: utf-8 -*- class A(object): """ Class A. """ 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 calss func.' 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 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__ 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__) 驗證,兩個確實不同。