首页 > 解决方案 > __init__() 缺少 7 个必需的位置参数

问题描述

我想将我的动物类继承给 Dog 类。一切都很好,直到我想在操作 1 中写 Dog.add_info()。我在第 46 行写了 Dog = Dog(animal) 但我认为有问题但我可以'不知道它是什么。我首先学习“类”的东西和“继承”的东西。

import random
class animal():

    def __init__(self,name,legs,place,move,weight,lenght):
        self.name = name
        self.legs = legs
        self.place = place
        self.move = move
        self.weight = weight
        self.lenght = lenght

    def __str__(self):
        return "Name: {}\nLegs: {}\nPlace: {}\nMove: {}\nWeight: {}\nLenght: {}".format(self,self.name,self.legs,self.place,self.move,self.weight,self.lenght)
    def add_info(self):
        info_name = input("Enter name")
        info_legs = input("Enter how many legs this animal have")
        info_place = input("Enter where this animal live")
        info_move = input("Enter how move this animal")
        info_weight = input("Enter Weight")
        info_lenght =input("Enter lenght")
        self.name = info_name
        self.legs = info_legs
        self.place = info_place
        self.move = info_move
        self.weight = info_weight
        self.lenght = info_lenght

class Dog(animal):

    def __init__(self,name,legs,place,move,weight,lenght,feather,aggresivity):
        super().__init__(self,name,legs,place,move,weight,lenght)
        self.feather = feather
        self.aggresivity = aggresivity
    def __str__(self):
        return "Name: {}\nLegs: {}\nPlace: {}\nMove: {}\nWeight: {}\nLenght: {}\nFeather: {}\nAggresivity {}".format(self, self.name, self.legs,self.place, self.move,self.weight, self.lenght,self.feather,self.aggresivity)
    def add_info(self):
        super().add_info()
        info_feather = input("Enter are your dog have feather or not 'have' or 'haven't")
        info_aggresivity =input("Enter are your dog aggresive or passive")
        self.feather = info_feather
        self.aggresivity = info_aggresivity
    def pick_random(self):
        list_dog = ["Labrador","Bulldog","Retriever,","Poodle","Beagle","Husky"]
        random_dog = random.choice(list_dog)
        print("Your dog is :",random_dog)
Dog = Dog(animal)
print("""
1 for add info to your dog
2 for get infos your dog have
3 for pick random dog type
q for quit
""")
choice = input("Enter operation: ")
while True:
    if (choice =="q"):
        print("BYE...")
        break
    elif(choice == "1"):
        Dog.add_info()

    elif(choice =="2"):
        pass

    elif(choice =="3"):
        pass

    else:
        print("İnvalid operation")

标签: pythonclass

解决方案


Doganimal期望所有与;相同的论点 您将类animal本身作为单个参数传递。

但是,与其复制的所有参数,不如Animal.__init__使用关键字参数来简化 的定义Dog.__init__

首先,我们会清理Animal一下。请注意,您self不必要地传递给许多方法,因为super()已经捕获了要传递的值。

class Animal:

    def __init__(self, *, name, legs, place, move, weight, length, **kwargs):
        super().__init__(**kwargs)
        self.name = name
        self.legs = legs
        self.place = place
        self.move = move
        self.weight = weight
        self.length = length

    def __str__(self):
        return "Name: {}\nLegs: {}\nPlace: {}\nMove: {}\nWeight: {}\nLength: {}".format(self.name, self.legs, self.place, self.move, self.weight, self.length)

    def add_info(self):
        info_name = input("Enter name")
        info_legs = input("Enter how many legs this animal have")
        info_place = input("Enter where this animal live")
        info_move = input("Enter how move this animal")
        info_weight = input("Enter Weight")
        info_length = input("Enter length")

        self.name = info_name
        self.legs = info_legs
        self.place = info_place
        self.move = info_move
        self.weight = info_weight
        self.length = info_length

现在我们Dog只定义额外的参数;任何用于超类方法的东西都将作为任意关键字参数Dog传递。

class Dog(Animal):

    def __init__(self, *, feather, aggresivity, **kwargs):
        super().__init__(**kwargs)
        self.feather = feather
        self.aggresivity = aggresivity

    def __str__(self):
        x = super().__str__()
        return x + "\nFeather: {}\nAggresivity {}".format(self.feather, self.aggresivity)

    def add_info(self):
        super().add_info()
        info_feather = input("Enter are your dog have feather or not 'have' or 'haven't")
        info_aggresivity =input("Enter are your dog aggresive or passive")
        self.feather = info_feather
        self.aggresivity = info_aggresivity

    def pick_random(self):
        list_dog = ["Labrador","Bulldog","Retriever,","Poodle","Beagle","Husky"]
        random_dog = random.choice(list_dog)
        print("Your dog is :",random_dog)

Dog最后,我们使用关键字参数进行实例化。

d = Dog(name="...", legs="...", ...) # etc

推荐阅读