1. 程式人生 > >Python中super()方法的使用

Python中super()方法的使用

如果在子類中也定義了構造器,既_init_()函式,那麼基類的構造器該如何呼叫呢?

方法一、明確指定 
使用一個子類的例項去呼叫基類的構造器,在子類的構造器中明確的指明呼叫基類的構造器。

class  C(P):
...     def __init__(self):
...             P.__init__(self)
...             print 'calling Cs construtor'
方法二、使用super()方法 
super()方法的漂亮之處在於,你不需要在定義子類構造器時,明確的指定子類的基類並顯式的呼叫,即不需要明確的提供父類,這樣做的好處就是,如果你改變了繼承的父類,你只需要修改一行程式碼(class程式碼行),而不需要在大量程式碼中去查詢那個要修改的基類。另外一方面程式碼的可移植性和重用性也更高。

>>> class  C(P):
...     def __init__(self):
...             super(C,self).__init__()
...             print 'calling Cs construtor'
... 
>>> c=C()
以上程式碼中P是C的父類。

 

Python類中super()和__init__()的關係

  

  1.單繼承時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()繼承時不用顯式引用基類。

 

  2. super()只能用於新式類中。

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

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

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

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

  3. 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

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

轉載請註明來源:開源中國 http://my.oschina.net/jhao104/blog/682322