Python的方法解析順序(MRO)

來源:互聯網
上載者:User

標籤:eal   順序   target   details   繼承   base   div   路徑   tail   

mro即method resolution order,主要用於在多繼承時判斷調的屬性的路徑(來自於哪個類)。

http://blog.csdn.net/imzoer/article/details/8737642

 

你真的理解Python中MRO演算法嗎?

 API:

 1 class type(object): 2     """ 3     type(object) -> the object‘s type 4     type(name, bases, dict) -> a new type 5     """ 6     def mro(self): # real signature unknown; restored from __doc__ 7         """ 8         mro() -> list 9         return a type‘s method resolution order10         """11         return []

 

測試代碼:

 1 #!/usr/bin/env python 2 class A(object): 3     def __init__(self): 4         print "enter A" 5         print("leave A") 6  7  8 class B(object): 9     def __init__(self):10         print "enter B"11         print "leave B"12 13 14 class C(A,B):15     def __init__(self):16         print "enter C"17         super(C, self).__init__()18         print "leave C"19 20 b = C()21 mo = b.__class__.mro()22 print mo

 

輸出:

enter C
enter A
leave A
leave C
[<class ‘__main__.C‘>, <class ‘__main__.A‘>, <class ‘__main__.B‘>, <type ‘object‘>]

Python的方法解析順序(MRO)

聯繫我們

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