This is probably something very simple and simple, and I probably just looked for errors for the wrong terms, but hopefully someone here can help me. (I'm still new to programming, which is probably obvious from this question.)
I am looking for a way to access variables from strings.
Like this:
A1 = {} B1 = {} C1 = {} mylist = ["A","B","C"] for blurb in mylist: blurb1.foo()
Now I use if / elif constructs in such cases:
if blurb == "A": A1.foo() elif blurb == "B": B1.foo() elif blurb == "C": C1.foo()
It works, but of course there is a more elegant way to do it?
Thanks for any help! Lastalda
Edit2: trying to clarify again (sorry for not being very consistent before, it's hard for me to get this through):
I would like to create and access objects from strings.
So, if I have a function that returns a string, I want to create, for example. list using this string as the name of the list, and then make stuff with this list.
x = foo()
Is there nothing more elegant than preparing lists for all possible outcomes of x and using long elif constructs or dictionaries? Should I use eval () in this case? (I hesitate to use eval () as it is considered dangerous.)
Hopefully this is finally clear. Any help is still appreciated!
source share