python Parent.__init()和super(Child, self)的區別

來源:互聯網
上載者:User

標籤:方法   一個   python3   return   col   html   log   turn   span   

super函數做的事情

def supper(cls, inst):    mro = inst.__class__.mro()    return mro[mro.index(cls) + 1]

inst產生父類的列表

從列表中找到cls的索引,然後返回其下一個索引

假設

mro = [a,b,c]

cls = b

那麼cls的索引為1

返回的是其下一個索引也就是mro[2]即c

mro 全程是Method Resolution Order

方法返回的是inst的所有的父類,

順序是

假設a是基類,b,c都是a的子類,d繼承了b和c(繼承順序是b,c)

那麼d的mro返回的是

[d,b,c,a]

對d使用super(d,self)方法返回的是b

更詳細內容請參看python cookbook

http://python3-cookbook.readthedocs.io/zh_CN/latest/c08/p07_calling_method_on_parent_class.html

python Parent.__init()和super(Child, self)的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.