首頁  >  文章  >  後端開發  >  python類別中super()和__init__()的區別

python類別中super()和__init__()的區別

WBOY
WBOY原創
2016-12-05 13:27:141387瀏覽

單繼承時super()和__init__()實現的功能是類似的

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()繼承時不用明確引用基底類別。

super()只能用於新式類別中

把基類改為舊式類,即不繼承任何基類

class Base():
def __init__(self):
print 'Base create'

執行時,在初始化b時就會報錯:

super(childB, self).__init__()
TypeError: must be type, not classobj

super不是父類,而是繼承順序的下一個類別

在多重繼承時會涉及繼承順序,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()的第一個參數可以是繼承鏈中任一個類別的名字,

如果是本身就會依序繼承下一個類別;

如果是繼承鏈裡之前的類別便會無限遞歸下去;

如果是繼承鏈裡之後的類別便會忽略繼承鏈匯總本身和傳入類別之間的類別;

例如將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

super()可以避免重複呼叫

如果childA基礎Base, childB繼承childA和Base,如果childB需要呼叫Base的__init__()方法時,就會導致__init__()被執行兩次:

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__()方法被執行了兩次

enter A 
Base create
leave A
Base create

使用super()是可避免重複呼叫

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__()的區別,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn