Question:
I know that some other languages, such as PHP, support a concept of “variable variable names” – that is, the contents of a string can be used as part of a variable name.I heard that this is a bad idea in general, but I think it would solve some problems I have in my Python code.
Is it possible to do something like this in Python? What can go wrong?
If you are just trying to look up an existing variable by its name, see How can I select a variable by (string) name?. However, first consider whether you can reorganize the code to avoid that need, following the advice in this question.
Best Answer:
You can use dictionaries to accomplish this. Dictionaries are stores of keys and values.append
, and other operations that would require awkward key management with a dict.If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com
Leave a Review