简体   繁体   中英

object.__init__() takes no parameters

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)

The error is here:

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

Additional, to @NPE's answer.

  • you're doing a class as super which is the metaclass, so then if you want that, why not have an empty super ?

  • That's all really :-)...

So code becomes:

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)

So instead of:

super(AbsBaseClass,self).__init__(y)

Do:

super(DerivedClass,self).__init__(y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM