您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)python類中super()和__init__()的區(qū)別是什么,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
今天來談?wù)剆uper()和——init_()它們之間的區(qū)別把,單列繼承的說法。
1、單繼承時(shí)super()和init()實(shí)現(xiàn)的功能是類似的
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()
輸出結(jié)果:
Base create creat A Base create creat B Base create
使用super()繼承時(shí)不用顯式引用基類。
把基類改為舊式類,即不繼承任何基類
class Base(): def __init__(self): print 'Base create'
執(zhí)行時(shí),在初始化b時(shí)就會(huì)報(bào)錯(cuò):
super(childB, self).__init__()TypeError: must be type, not classobj
在多重繼承時(shí)會(huì)涉及繼承順序,super()相當(dāng)于返回繼承順序的下一個(gè)類,而不是父類,類似于這樣的功能:
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__
輸入結(jié)果如下:
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和父類沒有關(guān)聯(lián),因此執(zhí)行順序是A —> B—>—>Base
執(zhí)行過程相當(dāng)于:初始化childC()時(shí),先會(huì)去調(diào)用childA的構(gòu)造方法中的 super(childA, self).init(), super(childA, self)返回當(dāng)前類的繼承順序中childA后的一個(gè)類childB;然后再執(zhí)行childB().init(),這樣順序執(zhí)行下去。
在多重繼承里,如果把childA()中的 super(childA, self).init() 換成Base.init(self),在執(zhí)行時(shí),繼承childA后就會(huì)直接跳到Base類里,而略過了childB:
enter A Base create leave A (<class '__main__.childC'>, <class '__main__.childA'>, <class '__main__.childB'>, <class '__main__.Base'>, <type 'object'>)
從super()方法可以看出,super()的第一個(gè)參數(shù)可以是繼承鏈中任意一個(gè)類的名字,
如果是本身就會(huì)依次繼承下一個(gè)類;
如果是繼承鏈里之前的類便會(huì)無限遞歸下去;
如果是繼承鏈里之后的類便會(huì)忽略繼承鏈匯總本身和傳入類之間的類;
比如將childA()中的super改為:super(childC, self).init(),程序就會(huì)無限遞歸下去。
如:
File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in __init__ super(childC, self).__init__() RuntimeError: maximum recursion depth exceeded while calling a Python object
如果childA基礎(chǔ)Base, childB繼承childA和Base,如果childB需要調(diào)用Base的init()方法時(shí),就會(huì)導(dǎo)致init()被執(zhí)行兩次:
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()
Base的init()方法被執(zhí)行了兩次
enter A Base create leave A Base create
使用super()是可避免重復(fù)調(diào)用
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__()的區(qū)別是什么了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。