標籤:影響 朋友 父類 靜態 code sel 全域 __init__ 對象
我們都知道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 = 3ll = []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): passclass 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): passp = 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__
Python __dict__屬性詳解