So I have been making an RPG game and run into some troubles with inheritance and unpacking.
I have tried to make a simple version of the problem I am having:
class Animal():
def __init__(self,name):
self.name = name
def say_noise(self):
noise = "I am an animal"
by_who = "Animal"
return noise,by_who
class Dog(Animal):
def __init__(self,name):
self.name = name
def say_noise(self):
Animal.say_noise(self)
animal = Animal("man")
doggy = Dog("name")
What I want to do is return the same 2 things in the Animal class and the Dog class.
Now when I do say_noise() with my Animal it works fine, as it returns:
('I am an animal', 'Animal')
However, it does not return anything when I do it with the Dog class, and I tried unpacking them as well (which is what I aim to do in my real code), but when I do this.
a,b =doggy.say_noise()
it just returns this:
TypeError: cannot unpack non-iterable NoneType object
How can I return the same thing with the same class method, by only inheriting it from the parent class?
It's because you are not returning anything from Dog classes say_noise method.
Update your code like this,
class Animal():
def __init__(self, name):
self.name = name
def say_noise(self):
noise = "I am an animal"
by_who = "Animal"
return noise, by_who
class Dog(Animal):
def __init__(self, name):
self.name = name
def say_noise(self):
return Animal.say_noise(self)
animal = Animal("man")
doggy = Dog("name")
a, b = doggy.say_noise()
Related
class Apple:
def print_my_tree_name(self):
print('I want to use name of Tree on which I\'m hanging')
class Tree:
def __init__(self, name):
self.name = name
self.apple = Apple()
a = Tree('test')
a.apple.print_my_tree_name()
Is it possible to access the name variable without passing self to the Apple class e.g. a.apple.print_my_tree_name()?
You can specify the name of the tree as an attribute of the apple in the tree constructor
class Apple:
def print_my_tree_name(self):
print('I am hanging on tree: %s'%self.tree_name)
class Tree:
def __init__(self, name):
self.name = name
self.apple = Apple()
self.apple.tree_name = name
a = Tree('test')
a.apple.print_my_tree_name()
Perhaps something like this, using a #staticmethod
class Tree:
def __init__(self, name):
self.name = name
self.apple = Apple()
class Apple():
#staticmethod
def print_my_tree_name():
print(f'Hanging on {a.name} tree.')
a = Tree('test')
a.apple.print_my_tree_name()
# Hanging on test tree.
I am not sure why you want to access name in Apple class but if I had to do this, I will implement it something as below
class Tree:
def __init__(self, name):
self.name = name
class Apple:
def __init__(self, name):
self.tree = Tree(name)
def print_my_tree_name(self):
print('I want to use %s'%self.tree.name)
a = Tree('test')
a.print_my_tree_name()
See composition in python that is what you need basically.
http://blog.thedigitalcatonline.com/blog/2014/08/20/python-3-oop-part-3-delegation-composition-and-inheritance/
My simplified code is below: it creates an animal, and places it inside a zoo. I want to print the list of animals within the zoo. Going round in circles with this!
class Animal(object):
def __init__(self, name):
self.name = name
class Zoo(object):
def __init__(self):
self.animals = []
def __str__(self):
rep = ", ".join(self.animals)
return rep
def add(self, name):
self.animals.append(Animal(name))
def main():
while True:
zoo = Zoo()
animal = input("add an animal: ")
zoo.add(animal)
print(zoo)
main()
The added __repr__ Method to the Animal returns us the name.
The zoo = Zoo() has to be outside of the loop, this makes sure that we do not create a new zoo with every iteration.
Then we print the list (zoo.animals).
class Animal(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
class Zoo(object):
def __init__(self):
self.animals = []
def __str__(self):
rep = ", ".join(self.animals)
return rep
def add(self, name):
self.animals.append(Animal(name))
def main():
zoo = Zoo()
while True:
animal = input("add an animal: ")
zoo.add(animal)
print(zoo.animals)
main()
You can simply refer to the name property of Animal in your Zoo.__str__(), e.g.:
def __str__(self):
return ', '.join(animal.name for animal in self.animals)
Now print(zoo) should work correctly.
However this doesn't provide a lot of encapsulation if say you wanted to change what it means to print an animal, e.g. height, size, etc. So perhaps a more encapsulated form would be:
class Animal(object):
...
def __str__(self):
return self.name
class Zoo(object):
...
def __str__(self):
return ", ".join(str(animal) for animal in self.animals)
Now when you print(zoo) the Animal class is responsible for its own string presentation.
Just as a note: you probably should create the Animal instance outside of Zoo, what happens if you decide to create a class hierarchy of Animals (e.g. Mammal) that has different behaviours, your Zoo class would only know about Animals.
class Animal(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
class Zoo(object):
def __init__(self):
self.animals = []
def __str__(self):
return ", ".join(str(animal) for animal in self.animals)
def add(self, animal):
self.animals.append(animal)
def main():
zoo = Zoo()
while True:
animal = Animal(input("add an animal: "))
zoo.add(animal)
print(zoo)
main()
This would still behave properly if you create a Mammal class:
class Mammal(Animal):
...
zoo.add(Mammal(...))
print(zoo)
Suppose I have a class:
class Person(object):
def __init__(self, name, hobbies):
self.name = name
self.hobbies = hobbies
... (and so on)
Now I want to initialise a child class, Employee, which extends person. I would like to initialise that class with an instance of the Person class. So I would like to do:
class Employee(Person):
def __init__(self, person, salary):
# Initialise the superclass from the given instance somehow
# I know I could do:
super(Employee, self).__init__(person.name, person.hobbies)
# But could I somehow do something like:
super(Employee, self).__init__(person)
# (In this case the difference is small, but it could
# be important in other cases)
# Add fields specific to an "Employee"
self.salary = salary
So that I can then call:
p1 = Person('Bob', ['Bowling', 'Skiing'])
employed_p1 = Employee(p1, 1000)
Is there any way I can do this, or do I explicitly have to call the parent class's constructor again?
Thanks a lot!
I thnk you want something like this:
class Person(object):
def __init__(self, name, hobbies):
self.name = name
self.hobbies = hobbies
def display(self):
print(self.name+' '+self.hobbies[0])
class Employee(Person):
def __init__(self, a, b =None,salary=None):
if b is None:
self.person = a
else:
self.person = Person(a,b)
self.name = self.person.name
self.hobbies = self.person.hobbies
self.salary = salary
bob = Employee('bob',['Bowling', 'Skiing'])
bob.display()
sue1 = Person('sue',['photography','music'])
sue2 = Employee(sue1,salary=123)
sue2.display()
I've added in the 'display' function just to make it easier to follow. Hope this helps.
I've tried reading a few different tutorials, but I still can't figure it out. I have two simple classes. Animal and cat.
class Animal:
def __init__(self, name):
self.name = name
class Cat(Animal):
def __init___(self, age):
self.age = age
print('age is: {0}'.format(self.age))
def talk(self):
print('Meowwww!')
c = Cat('Molly')
c.talk()
Output is:
Meowwww!
The code runs, but I'm a little confused. I created an instance of the cat class with c = Cat('Molly'). So somehow by using "Molly" as an argument for the Cat() class instance, it feeds "Molly" to the original base class (Animal) instead of the Cat class instance I created? Why? So how do I feed the Cat class instance the age variable it requires?
I tried doing:
c = Cat('Molly', 10)
But it complains about too many arguments. And secondly, why doesn't the __init__ function of the Cat class get called? It should print "age is...". It just never does.
EDIT: Got it to work, thanks to Martijn Pieters! Here is the updated code (works with python3):
class Animal():
def __init__(self, name):
self.name = name
print('name is: {0}'.format(self.name))
class Cat(Animal):
def __init__(self, name, age):
super().__init__(name)
self.age = age
print('age is: {0}'.format(self.age))
def talk(self):
print('Meowwww!')
c = Cat('Molly', 5)
c.talk()
You misspelled __init__:
def __init___(self, age):
# 12 345
That's 3 double underscores at the end, not the required 2.
As such, Python won't call it as it is not the method it is looking for.
If you want to pass in both age and name, give the method another argument, then call the parent __init__ with just the name:
class Cat(Animal):
def __init__(self, name, age):
super().__init__(name)
self.age = age
I am working on an assignment for Python Programming 157 at my school.
I need to write a class called Pet that has the following data attributes:
__name (for the name of the pet)
__animal_type (Examples: "Cat", "Dog", and "Hamster" )
__age (for the pet's age)
__height (for the pet's height)
It needs to include
set_name
get_name
I have tried like 4 times and cannot seem to get it right... any clues on getting it started?
# The Pet Program.
class PetProgram:
# The __init__ method accepts an argument for the program
# and adds it to the __pets attribute.
def __init__(self, pet):
self.__pets = pet
# The name will add to the pet attribute.
def name(self, name):
self.__pets = name
def age(self, age):
self.__pets = age
def animal(self, animal):
self.__pets = animal
def height(self, height):
self.__pets = height
# The pets_return will show you the list.
def pets_return(self):
return self.__pets
# The Pet Program.
import petsprogram
def main():
# Enter the name.
petname = input('What is the name of the pet: ')
print 'This will be added to the record.'
savings.name(petname)
# Display the list.
print petsprogram
main()
Above is my latest try...no such luck...any help? Thanks in advance...
A class is not a program, a class should model a thing, like a pet. Therefore, to start off, you should name your class appropriately.
class Pet(object): # Pet derives from the object class, always do this
Now I think you want a constructor that takes the name of the pet, and perhaps the type of pet as well, so we'll define that.
def __init__(self, pet_name, pet_type):
self.pet_name = pet_name
self.pet_type = pet_type
You also need a get and set for the name:
def get_name(self):
return self.pet_name
def set_name(self, pet_name):
self.pet_name = pet_name
To use this class, you instantiate it into an instance of the class:
puppy = Pet('Rover', 'Dog')
puppy.get_name() # prints Rover
I hope that's enough to get you going. You should read up on OOP in Python as mentioned in the comments.
First, why are you using "private" __attributes? That doesn't seem warranted.
Second, you're assigning all your properties to the same instance variable:
self.__pets = pet
self.__pets = name
self.__pets = age
self.__pets = animal
self.__pets = height
You should be using something like
def __init__(self, pet, name, age, animal, height):
self.pet = pet
self.name = name
self.age = age
self.animal = animal
self.height = height
and you should definitely read the tutorial on classes before venturing any further.
I'm not sure where the __pets binding comes into play. Your initial description doesn't say anything about that. I would not have expected an object that represents a pet, as in, a singular pet, to have an attribute that was a list of pets. I would expect something like:
class PetProgram:
def __init__(self, name ='', age=0, type='Unknown', height=0):
self.__name = name
self.__age = age
self.__animal_type = type
self.__height = height
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
I'm also not sure where the imports petprogram comes from... is that perchance what your supposed to call your module, and then the instructor has provided that as something you're supposed to run to test it?
(also, what's with all the __under_scores? is this something your teacher is encouraging?)
How does something like this look?
>>> class Pets:
def set_Name(self,name):
self.name=name
def get_Name(self):
return self.name
def set_Atype(self,atype):
self.atype=atype
def get_Atype(self):
return self.atype
def set_Age(self,age):
self.age=age
def get_Age(self):
return self.age
def set_Height(self,height):
self.height=height
def get_Height(self):
return self.height