Python: TypeError: __init__()只接受5个参数(给出6个参数)

时间:2021-12-24 04:45:32

I've been having this issue with this code, and I'm not sure why I'm still getting this TypeError here. I have the following Animal class, which is the base class for the Dog class defined below it. I want to create a Dog object which inherits from this Animal class.

我一直对这段代码有这个问题,我不确定为什么我还得到这个类型错误。我有下面的动物类,它是定义在它下面的狗类的基类。我想创建一个从这个动物类继承的Dog对象。

Here's the Animal class:

这里是动物的类:

class Animal(object):
    __name = None
    __height = None
    __weight = None
    __sound = None

    def __init__(self, name, height, weight, sound):
        self.__name = name
        self.__height = height
        self.__weight = weight
        self.__sound = sound

    def set_name(self, name):
        self.__name = name

    def set_height(self, height):
        self.__height = height

    def set_weight(self, height):
        self.__height = height

    def set_sound(self, sound):
        self.__sound = sound

    def get_name(self):
        return self.__name

    def get_height(self):
        return str(self.__height)

    def get_weight(self):
        return str(self.__weight)

    def get_sound(self):
        return self.__sound

    def get_type(self):
        print("Animal")

    def toString(self):
        return "{} is {} cm tall and {} kilograms and says {}".format(self.__name, self.__height, self.__weight, self.__sound)

And then I have the following Dog class which inherits from the above Animal class:

然后我有下面的狗类继承自上面的动物类:

class Dog(Animal):
    __owner = None

    def __init__(self, name, height, weight, sound, owner):
        self.__owner = owner
        super(Dog, self).__init__(self, name, height, weight, sound)

    def set_owner(self, owner):
        self.__owner = owner

    def get_owner(self):
        return self.__owner

    def get_type(self):
        print("Dog")

    def toString(self):
        return "{} is {} cm tall and {} kilograms and says {}. His owner is {}".format(self.__name, self.__height, self.__weight, self.__sound, self.__owner)

When I try to create a Dog object with the following code:

当我尝试用以下代码创建Dog对象时:

spot = Dog('Spot', 22, 11, 'Bark', 'John')

I get the following TypeError:

我得到以下类型错误:

TypeError: __init__() takes exactly 5 arguments (6 given)

Does anyone know why I'm getting this error? I'm not really understanding what's going on here.

有人知道我为什么会犯这个错误吗?我不太明白这是怎么回事。

Thanks in advance!

提前谢谢!

Edit: After taking out the self from the superclass, I now am getting an AttributeError: 'Dog' object has no attribute '_Dog__name' when I try the following code:

编辑:从超类中取出self后,我现在得到一个AttributeError:当我尝试以下代码时,'Dog'对象没有'_Dog__name'属性:

print(spot.toString())

1 个解决方案

#1


2  

Problem lays in __init__ method of superclass. super() call implicitly passes self as first argument. Simply use:

问题在于超类的方法。super()调用隐式地将self传递为第一个参数。简单的使用方法:

def __init__(self, name, height, weight, sound, owner):
    self.__owner = owner
    super(Dog, self).__init__(name, height, weight, sound)

Explicit self is necessary only when you refer to superclass by it's name.

只有当您通过超类的名称引用它时,才需要显式的self。

def __init__(self, name, height, weight, sound, owner):
    self.__owner = owner
    Animal.__init__(self, name, height, weight, sound)

#1


2  

Problem lays in __init__ method of superclass. super() call implicitly passes self as first argument. Simply use:

问题在于超类的方法。super()调用隐式地将self传递为第一个参数。简单的使用方法:

def __init__(self, name, height, weight, sound, owner):
    self.__owner = owner
    super(Dog, self).__init__(name, height, weight, sound)

Explicit self is necessary only when you refer to superclass by it's name.

只有当您通过超类的名称引用它时,才需要显式的self。

def __init__(self, name, height, weight, sound, owner):
    self.__owner = owner
    Animal.__init__(self, name, height, weight, sound)