python - __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")
解决方案
Dog
animal
期望所有与;相同的论点 您将类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
推荐阅读
- python - 如何使用不同的输入多次运行蜘蛛
- python - 如何从我的 Django 视图和 url 和控制台获取响应。使用 Javascript 在我的 Django 模板(home.html)中记录响应
- python - python中时间序列数据的螺旋图
- python - 跨多个文件的烧瓶 sqlalchemy 关系
- mysql - mysql docker中用户'root'@'localhost'的访问被拒绝
- nginx - 在 nginx 中停止(从位置退出)执行具有良好返回码的位置是真的吗?
- css - css模块中未生成的类名选择器
- linux - PPID 1 和错误的僵尸进程 - 无法获取属性:org.freedesktop.systemd1 的激活超时
- javascript - 输入字段中的第一个字母显示,其余字母不可见,如 ****** 如何在 javascript 和 typescript 中执行?
- google-cloud-platform - 有没有办法编写与目录中的文件或同一目录中的整个目录匹配的 glob 模式?