Python 调用super初始化报错 "super() argument 1 must be type, not classobj"

来源:互联网 发布:贪玩传奇盛世翅膀数据 编辑:程序博客网 时间:2024/06/05 12:38

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!!!

阅读全文
0 0
原创粉丝点击