Python 關於supper 的 用法和原理(挖坑),pythonsupper

來源:互聯網
上載者:User

Python 關於supper 的 用法和原理(挖坑),pythonsupper

一、前言

Python 物件導向中有繼承這個概念,初學時感覺很牛逼,裡面也有個super類,經常見到,最近做一些題才算是理解了。特地記錄分享給後來研究的小夥伴,畢竟現在小學生都開始學了(滑稽臉)

二、代碼

直接上乾貨,能把下面一個問題全答對,後面就不用看了。

class A():    def go(self):        print ("go A go!")    def stop(self):        print ("stop A stop!")    def pause(self):        raise Exception("Not Implemented")class B(A):    def go(self):        super(B, self).go()        print ("go B go!")class C(A):    def go(self):        super(C, self).go()        print ("go C go!")    def stop(self):        super(C, self).stop()        print ("stop C stop!")class D(B,C):    def go(self):        super(D, self).go()        print ("go D go!")    def stop(self):        super(D, self).stop()        print ("stop D stop!")    def pause(self):        print ("wait D wait!")class E(B,C):    passa = A()b = B()c = C()d = D()e = E()# 說明下列代碼的輸出結果a.go()print('--------')b.go()print('--------')c.go()print('--------')d.go()print('--------')e.go()print('--------')a.stop()print('--------')b.stop()print('--------')c.stop()print('--------')d.stop()print('--------')e.stop()print(D.mro())a.pause()b.pause()c.pause()d.pause()e.pause()

當然,直接運行就有答案了,還是要仔細想一下,反正看到我第一次跑出的結果的時候,我都不敢相信自己的眼睛。

 

step1:

幾個概念:

繼承的功能:父類的代碼重用

多態的功能:同一方法對不同類型的對象會有相應的結果

開閉原則:對擴充開放,對修改封閉

super類功能:新式類實現廣度優先的不重複的調用父類,解決了鑽石繼承(多繼承)的難題

 

step2:

super實現原理:通過c3演算法,產生mro(method resolution order)列表,根據列表中元素順序查詢調用

新式類調用順序為廣度優先,舊式類為深度優先

 

step3:

個人理解:

1.調用了父類的方法,出入的是子類的執行個體對象

2.新式類子類(A,B),A就在B之前

3.super類似於yield或遞迴的一種設計,當代碼執行到super執行個體化後,先去找同級父類,若沒有其餘父類,再執行自身父類,再往下走,

  簡潔點的三個原則就是:

子類在父類前,所有類不重複調用,從左至右

 

理解了以上的說法,題目就沒問題了。

也不用跑了,答案如下:

 

a.go()# go A go!b.go()# go A go!# go B go!c.go()# go A go!# go C go!d.go()# go A go!# go C go!# go B go!# go D go!e.go()# go A go!# go C go!# go B go!a.stop()# stop A stop!b.stop()# stop A stop!c.stop()# stop A stop!# stop C stop!d.stop()# stop A stop!# stop C stop!# stop D stop!e.stop()# stop A stop!a.pause()# ... Exception: Not Implementedb.pause()# ... Exception: Not Implementedc.pause()# ... Exception: Not Implementedd.pause()# wait D wait!e.pause()# ...Exception: Not Implemented

看了答案,其實還有一點,父類拋異常的情況,如果子類有不拋異常的方法,異常就不拋出了,這個設計也會很有用。

這裡就中間一個A,C,B,D的和網上常見的不太一樣,促使我仔細研究了一下,其實就是個人理解第三條。

 

 

 

補充:

Python2 和Python3在這個問題上的差別

 

Python2 沒有預設繼承object

Python3 預設全部繼承object類,都是新式類

 

Python2super調用 super(開始類名,self).函數名()

Python3  super().函數名()

 

關於調用父類函數傳入子類執行個體的栗子舉一個:

class A:    def __init__(self):        self.n = 2    def add(self, m):        print('self is {0} @A.add'.format(self))        self.n += mclass B(A):    def __init__(self):        self.n = 3    def add(self, m):        print('self is {0} @B.add'.format(self))        super().add(m)        print('newb')        self.n += 3class C(A):    def __init__(self):        self.n = 4    def add(self, m):        print('self is {0} @C.add'.format(self))        super().add(m)        print('newc')        self.n += 4class D(B, C):    def __init__(self):        self.n = 5    def add(self, m):        print('self is {0} @D.add'.format(self))        super().add(m)        self.n += 5d = D()d.add(2)print(d.n)

 

 

 

夜深了,暫時會這麼多就寫這麼多,有空研究c3原理(挖個坑先)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.