這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)python類中super()和__init__()的區(qū)別是什么,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
臺山網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),臺山網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為臺山上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的臺山做網(wǎng)站的公司定做!今天來談?wù)剆uper()和——init_()它們之間的區(qū)別把,單列繼承的說法。
1、單繼承時super()和init()實現(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()繼承時不用顯式引用基類。
把基類改為舊式類,即不繼承任何基類
class Base(): def __init__(self): print 'Base create'
執(zhí)行時,在初始化b時就會報錯:
super(childB, self).__init__()TypeError: must be type, not classobj
在多重繼承時會涉及繼承順序,super()相當(dāng)于返回繼承順序的下一個類,而不是父類,類似于這樣的功能:
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 (, , , , )
supder和父類沒有關(guān)聯(lián),因此執(zhí)行順序是A —> B—>—>Base
執(zhí)行過程相當(dāng)于:初始化childC()時,先會去調(diào)用childA的構(gòu)造方法中的 super(childA, self).init(), super(childA, self)返回當(dāng)前類的繼承順序中childA后的一個類childB;然后再執(zhí)行childB().init(),這樣順序執(zhí)行下去。
在多重繼承里,如果把childA()中的 super(childA, self).init() 換成Base.init(self),在執(zhí)行時,繼承childA后就會直接跳到Base類里,而略過了childB:
enter A Base create leave A (, , , , )
從super()方法可以看出,super()的第一個參數(shù)可以是繼承鏈中任意一個類的名字,
如果是本身就會依次繼承下一個類;
如果是繼承鏈里之前的類便會無限遞歸下去;
如果是繼承鏈里之后的類便會忽略繼承鏈匯總本身和傳入類之間的類;
比如將childA()中的super改為: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__() 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()方法時,就會導(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 [, , , ]
上述就是小編為大家分享的python類中super()和__init__()的區(qū)別是什么了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道。