激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 深入解析Python編程中super關鍵字的用法

深入解析Python編程中super關鍵字的用法

2020-08-29 10:53j_hao104 Python

Python的子類調用父類成員時可以用到super關鍵字,初始化時需要注意super()和__init__()的區別,下面我們就來深入解析Python編程中super關鍵字的用法:

官方文檔中關于super的定義說的不是很多,大致意思是返回一個代理對象讓你能夠調用一些繼承過來的方法,查找的機制遵循mro規則,最常用的情況如下面這個例子所示:

?
1
2
3
class C(B):
  def method(self, arg):
    super(C, self).method(arg)

子類C重寫了父類B中同名方法method,在重寫的實現中通過super實例化的代理對象調用父類的同名方法。

super類的初始方法簽名如下:

?
1
2
3
4
5
6
def __init__(self, type1, type2=None): # known special case of super.__init__
    """
    super(type, obj) -> bound super object; requires isinstance(obj, type)
    super(type) -> unbound super object
    super(type, type2) -> bound super object; requires issubclass(type2, type)
    Typical use to call a cooperative superclass method:

除去self外接受一個或者或者兩個參數,如同注釋聲明的一樣,接受兩個參數時返回的是綁定的super實例,省略第二個參數的時候返回的是未綁定的super對象。

一般情況下當調用繼承的類方法或者靜態方法時,并不需要綁定具體的實例,這個時候使用super(type, type2).some_method就能達到目的,當然super(type, obj)在這種情況下也能夠使用,super對象有自定義實現的getattribute方法也能夠處理。不過,后者一般用來調用實例方法,這樣在查找方法的時候能夠傳入相應的實例,從而得到綁定的實例方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class A(object):
  def __init__(self):
    pass
 
  @classmethod
  def klass_meth(cls):
    pass
 
  @staticmethod
  def static_meth():
    pass
 
  def test(self):
    pass
 
class B(A):
  pass
 
>>> b = B()
>>> super(B, b).test
<bound method B.test of <__main__.B object at 0x02DA3570>>
>>> super(B, b).klass_meth
<bound method type.klass_meth of <class '__main__.B'>>
>>> super(B, b).static_meth
<function static_meth at 0x02D9CC70>
>>> super(B, B).test
<unbound method B.test>
>>> super(B, B).klass_meth
<bound method type.klass_meth of <class '__main__.B'>>
>>> super(B,B).satic_meth
>>> super(B,B).static_meth
<function static_meth at 0x02D9CC70>

初始化super對象的時候,傳遞的第二個參數其實是綁定的對象,第一個參感覺數可以粗暴地理解為標記查找的起點,比如上面例子中的情況:super(B, b).test就會在B.__mro__里面列出的除B本身的類中查找方法test,因為方法都是非數據描述符,在super對象的自定義getattribute里面實際上會轉化成A.__dict['test'].__get__(b, B)。

super在很多地方都會用到,除了讓程序不必hardcode指定類型讓代碼更加動態,還有其他一些具體必用的地方比如元類中使用super查找baseclass里面的new生成自定義的類型模板;在自定義getattribute的時候用來防止無限循環等等。

關于super建議讀者將它與python的描述符一起來理解,因為super就實現了描述符的協議,是一個非數據描述符,能夠幫助大家更好的理解super的使用和工作原理。

同時,有以下4個點值得大家注意:
1、單繼承時super()和__init__()實現的功能是類似的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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()

輸出結果:

?
1
2
3
Base create
creat A Base create
creat B Base create


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

2、super()只能用于新式類中

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

?
1
2
3
class Base():
  def __init__(self):
    print 'Base create'

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

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

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

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

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

    mro()用來獲得類的繼承順序。

例如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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__

輸入結果如下:

?
1
2
3
4
5
6
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:

?
1
2
3
4
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__(),程序就會無限遞歸下去。

    如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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__()被執行兩次:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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()
?
1
2
3
4
enter A
Base create
leave A
[<class '__main__.childB'>, <class '__main__.childA'>, <class '__main__.Base'>, <type 'object'>]

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品一区二区三区免费 | 亚洲成人精品区 | 欧美一级免费在线观看 | 久久资源总站 | 精品亚洲国产视频 | 久久精品视频在线 | 亚洲综合91| 亚洲国产精品一 | 媚药按摩痉挛w中文字幕 | 成人毛片网站 | 欧美爱爱视频免费看 | 国产69精品久久99不卡免费版 | 久久激情国产 | 国产精品99精品 | 国产精品美女一区二区 | 视频一区二区三区在线 | 91精品国产日韩91久久久久久360 | 国产精品亚洲欧美 | 国产亚洲精品综合一区 | 欧美一区二区三区久久精品视 | 黄色影视免费看 | 1级黄色毛片 | 欧美在线观看黄色 | 精品亚洲视频在线 | 日韩毛片一区二区三区 | 精品无码一区在线观看 | 精品国产一区二区亚洲人成毛片 | 精品亚洲夜色av98在线观看 | 免费大香伊蕉在人线国产 | 在线观看免费av网 | 久久久久日本精品一区二区三区 | 日本娇小18xxxⅹhd | 久久久久久久九九九九 | 国产91丝袜在线播放0 | 免费观看黄视频 | 精品国产一区二区久久 | 久久日韩| 国产视频导航 | 欧美一级黄色片在线观看 | 成年人福利视频 | 国产亚洲自拍一区 |