Python __dict__屬性詳解
我們都知道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__) 驗證,兩個確實不同。