首页 > 代码库 > python中super出现的TypeError: must be type, not classobj 原因及解决

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

执行一下代码,出现错误,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 原因及解决