object .__ init __()不带参数

时间:2018-10-26 04:52:52

标签: python python-3.x abstract-class

import abc

class AbsBaseClass(abc.ABC):
    def __init__(self,x):
        self.x=x

class DerivedClass(AbsBaseClass):
    def __init__(self,y):
        super(AbsBaseClass,self).__init__(y)


ob=DerivedClass(5)

2 个答案:

答案 0 :(得分:2)

错误在这里:

class DerivedClass(AbsBaseClass):
    def __init__(self,y):
        super(AbsBaseClass,self).__init__(y)
 #            ^^^^^^^^^^^^ This should be DerivedClass, not AbsBaseClasee

答案 1 :(得分:0)

除了,@ NPE的答案。

  • 您正在做一个超类(即元类),所以如果您愿意,为什么不留空的super

  • 这真的很:-)...

因此代码变为:

import abc

class AbsBaseClass(abc.ABC):
    def __init__(self,x):
        self.x=x

class DerivedClass(AbsBaseClass):
    def __init__(self,y):
        super(DerivedClass,self).__init__(y) #!!!


ob=DerivedClass(5)

所以代替:

super(AbsBaseClass,self).__init__(y)

要做:

super(DerivedClass,self).__init__(y)