I want to Inheritance my animal class to Dog class.Everything is good until ı want to write Dog.add_info() at operation 1.I wrote Dog = Dog(animal) at line 46 but ı think there are a problem but ı can't find out what it is.I learning 'class' thing and 'Inheritance' thing first.
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")
CodePudding user response:
Dog
expects all the same arguments as animal
; you are passing the class animal
itself as a single argument.
Rather than duplicating all the arguments from Animal.__init__
, though, use keyword arguments to simplify the definition of Dog.__init__
.
First, we'll clean up Animal
a little. Note that you were passing self
unnecessarily to a lot of methods, as super()
already captures the value to pass.
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
Now we define Dog
with only the extra arguments; anything intended for the superclass methods will be passed as arbitrary keyword arguments that Dog
will pass on.
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)
Finally, we instantiate Dog
using keyword arguments.
d = Dog(name="...", legs="...", ...) # etc
CodePudding user response:
When initializing a class, you must provide all of the arguments required by the init method. See this great (and similarly themed) answer which illustrates the right way to initialize a instance:
class Dog:
def __init__(self, legs, colour):
self.legs = legs
self.colour = colour
fido = Dog(4, "brown")
spot = Dog(3, "mostly yellow")
In your case, you need to provide all of the required arguments: name, legs, place, move, weight, length, feather, aggresivity
.
If you'd like to write a function which helps a user instantiate an instance, you could create a classmethod:
class Animal:
...
@classmethod
def add_info(cls):
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")
return cls(
info_name,
info_legs,
info_place,
info_move,
info_weight,
info_lenght
)
now, calling Animal.add_info()
will prompt the user for the correct attributes and then return a properly instantiated object.
CodePudding user response:
This answer is based on the information in my comments above:
import random
class animal():
def __init__(self,name,legs,place,move,weight,length):
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,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
class Dog(animal):
def __init__(self, name="sparky", legs=4, place="right here", move=True, \
weight="45 pounds", length="30 inches", feather=False, aggresivity="friendly"):
super().__init__(name, legs, place ,move, weight, length)
self.feather = feather
self.aggresivity = aggresivity
def __str__(self):
return "Name: {}\nLegs: {}\nPlace: {}\nMove: {}\nWeight: {}\nlength: {}\nFeather: {}\nAggresivity {}".format(self, self.name, self.legs,self.place, self.move,self.weight, self.length,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()
print("""
1 for add info to your dog
2 for get infos your dog have
3 for pick random dog type
q for quit
""")
while True:
choice = input("Enter operation: ")
if (choice =="q"):
print("BYE...")
break
elif(choice == "1"):
Dog.add_info()
elif(choice =="2"):
pass
elif(choice =="3"):
pass
else:
print("İnvalid operation")
Let me know if you have any further questions.