Let's say I have a dictionary of dogs, where the key is the name of the dog and the value is an instance of a class Dog.
my_dogs = {'Ollie': Dog(...), 'Lassie': Dog(...)}
If I want to know my dogs I can access them by key
my_dogs['Ollie']
I would like however to have this structure as a class, something like
class MyDogs():
def __init__(self):
self.ollie = Dog()
self.lassie = Dog()
So I can access my dogs like:
my_dogs = MyDogs()
my_dogs.lassie
Using a dictionary I can create any names for my dogs by just setting the key to it's name, however in the class the variable is hardcoded, which is something that I don't want.
Question: Can I define a variable whose name is stored in another variable? Something like:
class MyDogs():
def __init__(self, dog_names):
self.eval(dog_names[0]) = Dog()
self.eval(dog_names[1]) = Dog()
This way MyDogs will have two instances named after the dog_names passed as parameters.
Is this possible? Is it a good practice or I should structure it differently?