I've been struggling for a couple of days now with the following...
I'm trying to find a way to instantiate a number of objects which I can name via a raw_input call, and then, when I need to, look at its attributes via the 'print VARIABLE NAME' command in conjunction with the str() method.
So, to give an example. Let's say I want to create a zoo of 10 animals...
class Zoo(object):
def __init__(self, species, legs, stomachs):
self.species = species
self.legs = legs
self.stomachs = stomachs
for i in range(9):
species = raw_input("Enter species name: ")
legs = input("How many legs does this species have? ")
stomachs = input("...and how many stomachs? ")
species = Zoo(species, legs, stomachs)
The idea is that the 'species' variable (first line of the for loop) e.g species = Bear becomes the object 'Bear' (last line of loop), which in conjunction with a str() method and the 'print Bear' command would give me the bears attributes.
Like I say, I've struggled for a while with this but despite looking at other posts on similar themes still can't figure out a way. Some say use dictionaries, others say use setattr() but I can't see how this would work in my example.