您的位置:首页 > 编程语言 > Python开发

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

2016-09-11 20:16 741 查看
执行一下代码,出现错误,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)后正常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  super python
相关文章推荐