I used created an array of objects belonging to a class. Each object has values that are unique to themselves. I need to find class a parameter's value belongs to.
For example
class Dog:
def __init__(self, name):
self.name = name
dogNames = ["Rex", "Otis", "Max"]
dogs = []
for dog in dogNames:
dogs.append(Dog(dog))
>>> dogs
[<__main__.Dog instance at 0x1181c5cf8>, <__main__.Dog instance at 0x117c7a050>, <__main__.Dog instance at 0x117d169e0>]
I would need to find which object has the name "Rex" assigned to it.
What is the most Pythonic way of doing this ?
if the names are unique, don't create a list, create a dictionary:
dog_names = ["Rex", "Otis", "Max"]
dogs = {name:Dog(name) for name in dog_names}
now you can perform fast dict lookup with default value to None if name not found:
rex_object = dogs.get("Rex")
if you want to keep your list, the same thing can be achieved (but with O(n) complexity with a generator expression and next defaulting to None if no dog is called "Rex":
next((d for d in dogs if d.name=="Rex"),None)
the most pythonicway is to use repr dunder method in your Dog class
class Dog:
def __init__(self,name):
self.name = name
def __repr__(self):
return 'dog name is {}'.format(self.name)
A simple way to do it is through the existing for loop that you have and just throw in an if statement to check if the name equals the name you're looking for and assign it to a variable.
find = ""
for dog in dogNames:
if dog.name == "Rex":
find = dog
Related
So I've been creating Person classes, which hold variables like name and age. I would then create multiple Person classes which have different variables unique to each class. What I want to add next is to give each Person a "relationship" variable shared between each of these classes.
For example:
The relationship between George and Alex is -31.
How would I approach this? I've thought about just making a variable for each object, but this doesn't seem convenient when there's just too much Person classes in the program.
First, I'm not sure you want to make each person a different class. The idea behind classes is that a class describes a type of object (like a Person), and then each Person is an instance of that type of object (like George).
So if your Person class is like:
class Person:
def __init__(name: str, age: int):
self.name = name
self.age = age
then you might have:
george = Person("George", 20)
alex = Person("Alex", 22)
Both george and alex are instances of Person, not their own class. This make sense so far?
Now, the way you'd add a relationship might be to have a dictionary that associates other Persons with this relationship score. While we're at it, let's add a __repr__ function to our class so we can pretty-print our people:
class Person:
def __init__(name: str, age: int):
self.name = name
self.age = age
self.relationships: Dict["Person", int] = {}
def __repr__(self) -> str:
return "%s, age %d" % (self.name, self.age)
Now you can do:
george.relationships[alex] = -31
alex.relationships[george] = -31
If we print out George's relationships, we get:
>>> print(george.relationships)
{Alex, age 22: -31}
And you can do this regardless of how many people and how many relationships between them you want to model (a dictionary can hold an arbitrary number of values associated with an arbitrary number of keys, which in this case is a Person instance).
Assuming that a person can have a "relationship" with more than one other person, you will need a dictionary to store all of the relationships that a person has. I suggest mapping the other person's name to the "relationship" number (-31 in your example).
class Person:
def __init__(self, name):
self.name
self.relationships = dict()
def set_relationship(self, other, value):
self.relationships[other.name] = value
other.relationships[self.name] = value
def get_relationship(self, other):
return self.relationships[other.name]
def has_relationship(self, other):
return other.name in self.relationships
The set_relationships method updates both objects' dictionaries, because the way you've described the relationship, it should be symmetric.
Usage:
>>> alice = Person('alice')
>>> bob = Person('bob')
>>> clive = Person('clive')
>>> alice.set_relationship(bob, 12)
>>> alice.relationships
{'bob': 12}
>>> bob.get_relationship(alice)
12
>>> clive.has_relationship(alice)
False
If you genuinely need each person to be represented by a different class (instead of just objects of the same class), you can write your other classes as subclasses of Person. That way you won't have to duplicate the code above across many different classes.
I want a method in my class that creates new objects from user-inputs and adds them to a dictionary (this may not be the best way of doing this, appreciate any input). Further I want to be able to still get the attributes of the objects in the dictionary for other functions in a simple way which to me is the hardest part.
I have looked around for any similar problems but cant seem to find any, while this issue seems pretty general to me I believe there is an easy way to do this or i'm just doing something very wrong. If i just add the objects to a dictionary I just the "position" of the object but I want the specific attributes added to the dictionary based on name and species as given below.
animals = {}
class animal:
def __init__(self,name,species):
self.name = name
self.species = species
def dict(self,name,species):
#i want this function to create and add new objects to animals
For a given user input of name and species I would like for the output of dict() to add the new object to a dictionary and then somehow for example being able to loop through the dictionary for the names of all animals in the dictionary. Thank you so much in advance.
I am not sure why you want to do this, but you can use a class method to effectively make a new constructor that also takes a dictionary and mutate that dictionary inside
class Animal:
def __init__(self, name, species):
self.name = name
self.species = species
#classmethod
def init_and_track(cls, dict_, name, species):
a = cls(name, species)
dict_[name] = a
and use as so
animals = {}
Animal.init_and_track(animals, 'sally', 'horse')
as for getting user inputs, you could instead do something like
#classmethod
def init_and_track(cls, dict_):
name = input('name: ')
species = input('species: ')
a = cls(name, species)
dict_[name] = a
where the name and species are made by the user upon calling this method. I am not sure exactly what you want and why you want it this way but this is what I believe you are asking for.
Your comment suggests your original question was an xy-problem - how about something like this. Define your animal using dataclasses because it is just a holder for some data (assuming python 3.7)
from dataclasses import dataclass
#dataclass
class Animal:
name: str
species: str
def say_hi(self):
print(f'{self.name} the {self.species} says hi')
the : <...> you see are type annotations. Then define a new class to keep track of these animals.
class Zoo:
def __init__(self):
self.animals = []
def add_animal(self):
print('adding a new animal to the zoo')
name = input('what is it\'s name? ')
species = input('what species is it? ')
a = Animal(name, species)
self.animals.append(a)
def find_by_name(self, name):
animals_with_name = [a for a in self.animals if a.name == name]
return animals_with_name
def find_by_species(self, species):
return [a for a in self.animals if a.species = species]
If you only wanted to keep track of one attribute like by species, you are right that a dictionary would be best. If you wanted to keep track of more attributes do something like
def find_by_attribute(self, attribute_name, value):
return [a for a in self.animals if getattr(a, attribute_name) == value]
to avoid writing duplicate code. This should help get you started
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.
Is it OK to have 2 constructor functions, the regular __init__ and a #classmethod Animal.get?
Since creating a new object is computationally intensive, we want to store previously created instances in a class attribute cls.zoo and get a cached copy of the instance from cls.zoo if it exists.
The user will not access Animal.zoo directly. If the user wants to get an Animal object, he will always use Animal.get().
Is this approach proper/pythonic?
I'm not familiar with the Singleton Pattern. Is the code considered using the Singleton Pattern?
class Animal:
zoo = {}
# will not be called directly from outside the class
def __init__(self, species ,age):
self.species = species
self.age = age
self.runExpensiveFunction()
# User alway use this function
#classmethod
def get(cls, species):
if species in cls.zoo:
animal = cls.zoo[species]
else:
animal = Animal(species, 0)
cls.zoo[species] = animal
return animal
tiger = Animal.get('tiger')
bear = Animal.get('bear')
It depends whether you just want to allow the user of your class to access a cached object, or if you want to force it to only access that cached object. With you solution, user can always use tiger2 = Animal('tiger', 0) to get another instance.
If you really want only one instance, you can use __new__:
class Animals(object):
zoo = {}
def runExpensiveFunction(self):
print "EXPENSIVE CALLED"
def __new__(cls, species):
if species in cls.zoo:
return cls.zoo[species]
else:
animal = object.__new__(Animals)
animal.species = species
animal.age = 0
animal.runExpensiveFunction()
Animals.zoo[species] = animal
return animal
Here is the proof that you can only create one instance:
>>> tiger1 = Animals('tiger')
EXPENSIVE CALLED
>>> tiger2 = Animals('tiger')
>>> tiger2 is tiger1
True
Suppose I have something like:
class Dog:
name = ""
weight = 0
def __init__(self,name,wg):
self.name = name
self.wg = wg
def feed(pet):
pet.weight = pet.weight + 5
if pet.weight > 20:
print("your dog would get too fat")
else:
print("it is safe to feed your dog")
my_dog = Dog("sparky",10)
feed(my_dog)
Is there anyway to have it so that the changes to pet.weight are not transferred to my_dog.weight?
I want the function to be able to take information from a class instance, perform calculations, and then leave the original instance unchanged.
You are reassigning the value of the pet.weight attribute on this line:
pet.weight = pet.weight + 5
Doing this affects the pet instance even after the code has returned from the feed function.
To fix the problem, simply create a local name that is equal to pet.weight + 5 and work with that instead:
def feed(pet):
weight = pet.weight + 5
if weight > 20:
print("your dog would get too fat")
else:
print("it is safe to feed your dog")
Now, we use the value of pet.weight but avoid changing it.
Also, you have defined your class incorrectly. By doing this:
class Dog:
name = ""
weight = 0
you have made name and weight to be class attributes of Dog. This means that their values will be shared among all instances of Dog and modifying them will affect all instances. In other words, all of your Dogs will have the same name and weight.
But then you do:
def __init__(self,name,wg):
self.name = name
which overshadows the class attribute name with an instance attribute of the same name. Unlike class attributes, instance attributes are unique to each instance of a class. So, weight is still a class attribute (shared by all Dogs) while name is now an instance attribute (unique to each Dog).
I think it would be best if you removed those lines and wrote the Dog class like so:
class Dog:
def __init__(self, name, weight):
self.name = name
self.weight = weight
Now, name and weight are both instance attributes. Each Dog has a unique name and weight and changing either of those values on one Dog will no longer affect the other Dogs.
For more information on this, you can check out these sources:
https://docs.python.org/3/tutorial/classes.html
http://www.tutorialspoint.com/python/python_classes_objects.htm