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

單繼承時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()

輸出結果:code

Base create
creat A  Base create
creat B  Base create

區別是使用super()繼承時不用顯式引用基類。繼承

super()只能用於新式類中

把基類改成舊式類,即不繼承任何基類遞歸

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

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

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

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

在多重繼承時會涉及繼承順序,super()至關於返回繼承順序的下一個類,而不是父類,相似於這樣的功能:io

def super(class_name, self):
    mro = self.__class__.mro()
    return mro[mro.index(class_name) + 1]

mro()用來得到類的繼承順序。
例如:class

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—>—>Baseobject

執行過程至關於:初始化childC()時,先會去調用childA的構造方法中的 super(childA, self).__init__(), super(childA, self)返回當前類的繼承順序中childA後的一個類childB;而後再執行childB().__init()__,這樣順序執行下去。sso

在多重繼承裏,若是把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'>]
相關文章
相關標籤/搜索