Change attribute in multiple objects - python

I'm learning OOP in Python and I get stucked with one thing.
I have an example class:
class Animal:
def __init__(self, name="", hunger=0):
self.name = name
self.hunger = hunger
def eat(self):
self.hunger += 1
And some objects:
dog = Animal("dog")
cat = Animal("cat")
giraffe = Animal("giraffe")
I would like to use method eat() to change value of hunger in every single one of them at one blow. I have already tried to do something like this:
Animal.eat()
But it doesn't work (there's TypeError, because of missing argument 'self').
Also:
Animal.hunger += 1
Doesn't work (returns AttributeError).
If anyone has any ideas, I would be very grateful!

You can maintain a class variable that collects the instances and adjust all of their hungers in eat:
class Animal:
instances = []
def __init__(self, name="", hunger=0):
self.name = name
self.hunger = hunger
Animal.instances.append(self)
def eat(self):
for i in Animal.instances:
i.hunger += 1
Semantically, you might want to make it a classmethod, though
#classmethod
def eat(cls):
for i in cls.instances:
i.hunger += 1
You can still call it on instances if you so wish.

#schwobaseggi has the most straightforward answer for what you want to do, but what you want to do seems like it's asking for trouble. You have one class that does two very different things. Animal is an animal that has a name and eats, and it also keeps track of every animal instance and makes all of them eat. Animal is trying to do what individual animals do and also control a group of animals.
It might be better to split this into two different kinds of objects: An animal, and some sort of AnimalGroup like Zoo or Farm or Herd. The AnimalGroup class should be responsible for keeping track of a bunch of instances and make them all do stuff.
class AnimalGroup(object):
def __init__(self, animal_list):
self.animals = animal_list[:]
def add_animal(self, animal):
self.animals.append(animal)
def all_eat(self):
for animal in self.animals:
animal.eat()
then
dog = Animal("dog")
cat = Animal("cat")
giraffe = Animal("giraffe")
group = AnimalGroup([dog, cat, giraffe])
group.all_eat()
group.add_animal(Animal("pig"))
group.all_eat()
This separates out the responsibilities of each class and makes things much easier to change later on. You can now have different group behaviors without ever needing to change the animal class. You can have new animal classes that inherit from Animal and you don't need to worry about side effects. for example: class Mammal(Animal) . When I call Mammal.eat, will it update all animals? It might. class variables can be a bit tricky like that. Should it update all animals? No idea. With an AnimalGroup object, you don't need to worry.

You actually have to call it on the object itself like this:
cat.eat()
dog.eat()
giraffe.eat()
otherwise it doesn't know which object to actually change. You could store all your Objects in an array and loop over that array to call the function on all of them one after another:
dog = Animal("dog")
cat = Animal("cat")
giraffe = Animal("giraffe")
animals=[dog, cat, giraffe]
for animalType in animals:
animalType.eat()
now you can do them all at once or one at a time if you want. You will however need to addnew animals to the array after you create them to keep the list up to date:
fish=new Animal("fish")
animals.append(fish)

class Animal(object):
hunger = 0
def __init__(self, name=""):
self.name = name
def eat(self):
Animal.hunger = Animal.hunger + 1
dog = Animal("dog")
cat = Animal("cat")
giraffe = Animal("giraffe")
dog.eat()
print("Dog's hunger variable is", dog.hunger)
1
dog.eat()
print("Dog's hunger variable is :",dog.hunger)
2
print("Cat's hunger variable is :",cat.hunger)
2
print("Giraffe's hunger variable is :", giraffe.hunger)
2
When eat() is called on a single instance, the hunger variable is updated for all instances!

If you're wanting to do something on the class you have to declare it as a class variable:
class Animal:
hunger = 0
def __init__(self, name=""):
self.name = name
#classmethod
def eat(klass):
klass.hunger += 1
This way anytime you call Animal.eat() you'll be referencing the class method that modifies your class variable. You can still access the variable from within an Animal class with self.hunger but I would advise against that as it can get confusing coming back and trying to determine what's a class variable and what's a member variable.

To the best of my knowledge (and I really like OOP in python), the only way to do this is to create a new class with that specific attribute a.k.a.
class Animals:
def __init__(self, animals):
self.animals = animals
def all_eat(self):
for animal in animals:
animal.eat()
Then what you would have to do is:
dog = Animal("dog")
cat = Animal("cat")
giraffe = Animal("giraffe")
animals = Animals((dog, cat, giraffe))
animals.all_eat()
The reason for this is that python classes themselves do not have callable attributes so you have to call each instance of the class separately.

Related

Python: pass a class-function as a reference to an external function

I have a class with several functions.
From outside that class, I want to specify with a reference which function to call - but I'm not sure how.
For example, I have an Animal class with two functions sound and food. I want to write a Zoo class which receives one of the Animal's functions as input and applies that function to every animal instance it holds (the function all_animals_features).
class Animal:
def __init__(self, sound, food):
self.my_sound = sound
self.my_food = food
def sound(self):
# Do some complicated stuff....
return self.my_sound
def food(self):
return self.my_food
class Zoo():
def __init__(self, animals):
self.animals = animals
def all_animals_features(self, f):
return [animal.f() for animal in self.animals]
dog = Animal('Woof', 'Bone')
cat = Animal('Meow', 'Cream')
zoo = Zoo([cat, dog])
zoo.all_animals_features(Animal.sound)
But of course, 'Animal' object has no attribute 'f'.
Any idea how can this be implemented?
Clarification: if all that is needed, as demonstrated by this silly example, is just getting an attribute then it may be simpler to use getattr().
In your case you just need to adjust the way the method is called:
class Zoo():
def __init__(self, animals):
self.animals = animals
def all_animals_features(self, f):
return [f(animal) for animal in self.animals]
dog = Animal('Woof', 'Bone')
cat = Animal('Meow', 'Cream')
zoo = Zoo([cat, dog])
print(zoo.all_animals_features(Animal.sound))
Output:
['Meow', 'Woof']
Since you supply Animal.sound, as parameter f, the call in the list comprehension is: f(animal)

how to call a the class from a method within the class in python to redefine a variable?

I'm having some issues redefining an object that I created by using a method within the same class that defines such object. I have written a small python example. Here I'm trying to create an object called dog3 using a method in the class dog that calls itself in order to change its dog3.getDogName. When I print the dog3 name, the call to init did not took effect. Does anyone knows how to perform this operation?
I expect to get an output as
woofy
max
bear
but instead of bear I get woofy again.
import sys
class Dog():
def __init__(self, name = 'woofy'):
self.name = name
def getDogName(self):
return self.name
def newDog(self, new_name):
return Dog(new_name)
class Animals():
def __init__(self, *args):
dog1 = Dog()
print(dog1.getDogName())
dog2 = Dog('max')
print(dog2.getDogName())
dog3 = dog1
dog3.newDog('bear')
print(dog3.getDogName())
def mainApp(args):
global app
app = Animals(args)
if __name__ == "__main__":
mainApp(sys.argv)
I'm sure an experience python programer would know how to do an operation like this.
Thank you for your help in advance.
Your code have defined the method newDog to return a new instance of Dog.
Your code also have dog3 being assigned an instance of Dog, but when your code called dog3.newDog(...) the return value is not assigned to anything. so the new Dog instance that got created went nowhere.
You might want to consider doing this instead.
dog3 = dog1.newDog('bear')
print(dog3.getDogName())
newDog is creating a new dog and not modifying the old one
If you want newDog to return a new dog, then do this:
dog3 = dog1.newDog("bear")
or really you should just be doing
dog3 = Dog("bear")
If you want newDog to modify the current Dog instance, do this:
def renameDog(self, new_name):
self.name = new_name
Don't make instance constructors unless you want to clone certain parameters. It can get confusing.
The method NewDog "return" a new Dog instance, but it will not change what it is, so if u want to change the name of current Dog instance, do this:
class Dog():
def change_name(self, new_name):
self.name = new_name
if u want to get another Dog instance, do this:
class Dog(object):
#staticmethod
def new_dog(new_name):
return Dog(new_name)

Modify Class Object Argument Based On When Calling A Method

This is a heavily simplified version of what I am working on, I just don't want to put 5,000 lines in here. So I know this works and all, but I want to be able to have the method"eat" be able to be applied non-specifically to any object that this class parents (such as "John Smith", and adding lets say "Mike Doe".) I would like it to automatically select the person who undergoes ".eat()" to eat food rather than making the method specifically state: johnSmith.hunger = False. What I am doing is creating methods of actions people can use within the game that affect other objects (class children and variables), but I don't want to set a method for each action for each character unit. That would be insane. Given below is my set of code.
class human():
def __init__(self, name, hunger):
self.name = name
self.hunger = hunger
def eat(self):
johnSmith.hunger = False
print("Human Ate Food")
johnSmith = human("John Smith", True)
print("Human Is Hungry:", johnSmith.hunger)
johnSmith.eat()
print("Human Is Hungry:", johnSmith.hunger)
If I am unclear (which I know I am Not doing a great job explaining), feel free to ask.
Just use self:
def eat(self):
self.hunger = False
print("%s Ate Food" % self.name)

Prepopulating instances in code for classes (Python)

I am trying to make it so that when I can run a function which would populate a class with a couple of instances which are contained within the code.
class Pets(object):
def __init__(self, name, scientific_name, feet_number, type)
super(Pets,self).__init__()
self.name = name
self.scientific_name = scientific_name
self.feet_number = feet_number
self.type = type
This is the point where I get stuck.
I want to make a function which has a list of instances (Ex. a dog, a cat, a horse...) so that when the function is run those instances can be accessed immediately.
I know from places like Creating dynamically named variables from user input (Second Paragraph, First Sentence), that what I'm asking for is possible, I just don't know the syntax for it.
Is this what you are trying to do?
class Pet(object):
def __init__(self, details):
self.name = details[0]
self.scientific_name = details[1]
self.feet_number = details[2]
self.type = details[3]
if __name__ == '__main__':
pet_list = [('Cat', 'Kitty Cat', 4, 'Cuddly'), ('Dog', 'Puppy Wuppy', 3, 'Licky')]
pets = [Pet(item) for item in pet_list]
Which gives you:
pets
> [<__main__.Pet at 0x8134d30>, <__main__.Pet at 0x8134d68>]
pets[0]
> <__main__.Pet at 0x8134d30>
pets[0].name
> 'Cat'
pets[0].scientific_name
> 'Kitty Cat'
pets[1].name
> 'Dog'
There are a lot of ways this could be put together depending on what you want to do. For example, you could make a master class called Pet() with some basic attributes and methods that are true for all pets, then create specific classes for each pet that inherit the base class, e.g. class Cat(Pet):
Or you could give the Pet class the ability to know all the other details depending on what name is passed into it, then populate the instance variables accordingly.

How to effectively save referenced class type in Python? (appengine)

I'd like to refer to different classes in a persistent appengine Model. Like:
class Animal():
def eat(self, food):
return food - 1
class Cat(Animal):
def eat(self, food):
return food - 2
class Dog(Animal):
def eat(self, food):
return food - 3
class Person(db.Model):
name = db.StringProperty()
pet = Animal()
I want to be able to assign either a Cat() or Dog() instance as a pet, save it and be able to reload a Person object with the original type of animal referenced in it. Persistance for the pet objects are not important, I only want to define different sets of behaviours with the methods of the classes. How do I do this?
Thank you!
Check out the PolyModel class.
The best I can come up with myself is a getter method (assuming the above Animal, Cat, Dog classes):
class Person(db.Model):
name = StringProperty()
pet = StringProperty(choices=('cat','dog'))
def get_pet(self):
dict = { 'cat': Cat, 'dog': Dog }
return dict[self.pet]
This way I have to maintain the list of available pet types in two additional places (choices for the saved string variable and the dictionary of string-class pairs). Is there a more risk free and elegant way to do this?

Categories