码迷,mamicode.com
首页 > 编程语言 > 详细

python中super出现的TypeError: must be type, not classobj 原因及解决

时间:2016-09-11 23:12:37      阅读:645      评论:0      收藏:0      [点我收藏+]

标签:python中的super

执行一下代码,出现错误,TypeError: must be type, not classobj


class A():
    def __init__(self):
        print("Enter A")
        print("Leave A")

class B(A):
    def __init__(self):
        print("Enter B")
        super(B, self).__init__()
        print("Leave B")

class C(A):
    def __init__(self):
        print("Enter C")
        super(C, self).__init__()
        print("Leave C")

class D(A):
    def __init__(self):
        print("Enter D")
        super(D, self).__init__()
        print("Leave D")

class E(B, C, D):
    def __init__(self):
        print("Enter E")
        super(E, self).__init__()
        print("Leave E")

E()

输出错误:

Traceback (most recent call last):
  File "F:/test5.py", line 35, in <module>
    E()
  File "F:/test5.py", line 32, in __init__
    super(E, self).__init__()
TypeError: must be type, not classobj

百度之后发现,python中super只能应用于新类,而不能应用于经典类

见:http://blog.csdn.net/andos/article/details/8973368


使用新式类(即A继承自object)后正常。


本文出自 “Farley” 博客,请务必保留此出处http://10250691.blog.51cto.com/10240691/1851741

python中super出现的TypeError: must be type, not classobj 原因及解决

标签:python中的super

原文地址:http://10250691.blog.51cto.com/10240691/1851741

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!