This is probably something very basic and simple, and I'm probably just googling for the wrong terms, but hopefully someone here can help me. (I'm still a beginner to programming, which is probably obvious from this question.)
I'm looking for a way to access variables from strings.
Like this:
A1 = {}
B1 = {}
C1 = {}
mylist = ["A","B","C"]
for blurb in mylist:
blurb1.foo()
Right now I'm using if/elif-constructions in such cases:
if blurb == "A":
A1.foo()
elif blurb == "B":
B1.foo()
elif blurb == "C":
C1.foo()
That works, but surely there's a more elegant way of doing it?
Thanks for any help! Lastalda
Edit2: trying to clarify again (sorry for not being very coherent before, it's hard for me to get this across):
I would like to create and access objects from strings.
So if i have a function that returns a string, I want to create e.g. a list using this string as the list name, and then do stuff with that list.
x = foo() # foo returns a string, e.g. "A"
# create list named whatever the value of x is
# in this case:
A = []
Isn't there anything more elegant than preparing lists for all possible outcomes of x and using long elif constructions or dictionaries? Do I have to use eval() in that case? (I'm hesitant to use eval(), as it's considered dangerous.)
I hope it's finally clear now. Any help still appreciated!