Python 調用super初始化報錯 "super() argument 1 must be type, not classobj"__Python

來源:互聯網
上載者:User

python3.0不存在,舊版本可能報錯:

class A():    def __init__(self):        print('A')class B():    def __init__(self):        print('B')class C():    def __init__(self):        print('C')class Asub(A):    def __init__(self):        print('Asub')        super(Asub, self).__init__()class Bsub(B):    def __init__(self):        print('Bsub')        super(Bsub,self).__init__()class Csub(C):    def __init__(self):        print('Csub')        super(Csub, self).__init__()class E(Asub, Bsub, Csub):    def __init__(self):        print ('E')        Asub.__init__(self)        Bsub.__init__(self)        Csub.__init__(self)d = E()

在子類Asub中調用super初始化時發生錯誤:其中A為超類,仔細檢查並無語法錯誤。 super(Asub, self).init() TypeError: super() argument 1 must be type, not classobj 原因如下: 在python2.2版本之前,直接調用超類的方法,後來改成通過super來調用,原因是為瞭解決多重繼承中的鑽石形狀問題。python裡的super只能用在新式類中,不能用於以前的經典類,如果基類是經典類則會出現這個錯誤。 解決的方法是Asub只要有一個超類是Object就OK了。 例如: A(object):…………….

class A(object):#!!!!!!!based in object!!!!!!    def __init__(self):        print('A')class B(object):    def __init__(self):        print('B')class C(object):    def __init__(self):        print('C')class Asub(A):    def __init__(self):        print('Asub')        super(Asub, self).__init__()class Bsub(B):    def __init__(self):        print('Bsub')        super(Bsub,self).__init__()class Csub(C):    def __init__(self):        print('Csub')        super(Csub, self).__init__()class E(Asub, Bsub, Csub):    def __init__(self):        print ('E')        Asub.__init__(self)        Bsub.__init__(self)        Csub.__init__(self)d = E()

注意:構造類的時候,習慣性繼承基類object!!!

聯繫我們

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