TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for bases A2, A1 出現原因及其解決辦法

來源:互聯網
上載者:User

標籤:pass   先後   ==   根據   報錯   ann   bar   err   error   

原本想測試繼承,出現了這個錯誤:

  原始碼

 1 class A1(object): 2     def fo1(self): 3         print "i‘m A1" 4 class A2(object): 5     def fo1(self): 6         print "i‘m A2" 7 class B1(A1,A2): 8     def bar(self): 9         print "i‘m B1"10 class B2(A2,A1):11     def bar(self):12         print "i‘m B2"13 class C(B1,B2):#提示出現錯誤的地方14     pass15 if __name__==‘__main__‘:16     m=C()17     print C.__mro__18     m.bar()19     m.fo1()

錯誤行數出現在13行,實際就是構圖的時候產生了錯誤,(根據繼承先後廣搜構圖)

此時構圖為

顯然不能出現倆A1,因此會出現如上的報錯

要B都與A有聯絡只能是下面這種繼承構圖才不會出錯

即將代碼修改為

class A1(object):    def fo1(self):        print "i‘m A1"class A2(object):    def fo1(self):        print "i‘m A2"class B1(A1,A2):    def bar(self):        print "i‘m B1"class B2(A1,A2):    def bar(self):        print "i‘m B2"class C(B1,B2):    passif __name__==‘__main__‘:    m=C()    print C.__mro__    m.bar()    m.fo1()

 

TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for bases A2, A1 出現原因及其解決辦法

相關文章

聯繫我們

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