歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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()

輸出結果:

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'>]

Copyright © Linux教程網 All Rights Reserved