class Base(object): def init(self): print 'Base create' class childA(Base): def init(self): print 'creat A ', Base.init(self) class childB(Base): def init(self): print 'creat B ', super(childB, self).init() base = Base() a = childA() b = childB()
輸出結果:
Base create creat A Base create creat B Base create
差異是使用super()繼承時不用明確引用基底類別。
#把基類改為舊式類,即不繼承任何基底類別
class Base(): def init(self): print 'Base create'
執行時,在初始化b時就會報錯:
super(childB, self).init() TypeError: must be type, not classobj
在多重繼承時會涉及繼承順序,super()相當於返回繼承順序的下一個類,而不是父類,類似於這樣的功能:
def super(class_name, self): mro = self.class.mro() return mro[mro.index(class_name) + 1]
mro()用來得到類別的繼承順序。 例如:
class Base(object): def init(self): print 'Base create' class childA(Base): def init(self): print 'enter A ' # Base.init(self) super(childA, self).init() print 'leave A' class childB(Base): def init(self): print 'enter B ' # Base.init(self) super(childB, self).init() print 'leave B' class childC(childA, childB): pass c = childC() print c.class.mro
輸出結果如下:
enter A enter B Base create leave B leave A (<class 'main.childC'>, <class 'main.childA'>, <class 'main.childB'>, <class 'main.Base'>, <type 'object'>)
supder和父類別沒有關聯,因此執行順序是A —> B—>—>Base
#執行過程相當於:初始化childC()時,先會去呼叫childA的建構方法中的super(childA, self).init(), super(childA, self)傳回目前類別的繼承順序中childA後的一個類別childB;然後再執行childB().init(),這樣順序執行下去。
在多重繼承裡,如果把childA()中的super(childA, self).init() 換成Base._init_(self),在執行時,繼承childA後就會直接跳到Base類別裡,而略過了childB:
enter A Base create leave A (<class 'main.childC'>, <class 'main.childA'>, <class 'main.childB'>, <class 'main.Base'>, <type 'object'>)
從super()方法可以看出,super()的第一個參數可以是繼承鏈中任意一個類別的名字,
如果是本身就會依序繼承下一個類別;
遞歸下去;
File "test.py", line 12, in init super(childC, self).init() File "test.py", line 12, in init super(childC, self).init() File "test.py", line 12, in init super(childC, self).init() File "test.py", line 12, in init super(childC, self).init() File "test.py", line 12, in init super(childC, self).init() File "test.py", line 12, in init super(childC, self).init() File "test.py", line 12, in init super(childC, self).init() File "test.py", line 12, in init super(childC, self).init() File "test.py", line 12, in init super(childC, self).init() RuntimeError: maximum recursion depth exceeded while calling a Python object
class Base(object): def init(self): print 'Base create' class childA(Base): def init(self): print 'enter A ' Base.init(self) print 'leave A' class childB(childA, Base): def init(self): childA.init(self) Base.init(self) b = childB()
enter A Base create leave A Base create
class Base(object): def init(self): print 'Base create' class childA(Base): def init(self): print 'enter A ' super(childA, self).init() print 'leave A' class childB(childA, Base): def init(self): super(childB, self).init() b = childB() print b.class.mro() enter A Base create leave A [<class 'main.childB'>, <class 'main.childA'>, <class 'main.Base'>, <type 'object'>]
#
以上是關於python類別中super()和__init__()的區別說明的詳細內容。更多資訊請關注PHP中文網其他相關文章!