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?
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.)