How do I accomplish variable variables in Python?
Here is an elaborative manual entry, for instance: Variable variables
I hear this is a bad idea in general though, and it is a security hole in PHP. Is that true?
How do I accomplish variable variables in Python? Here is an elaborative manual entry, for instance: Variable variables I hear this is a bad idea in general though, and it is a security hole in PHP. Is that true? |
|||||||||||||||||||||
|
Use dictionaries to accomplish this. Dictionaries are stores of keys and values.
You can use variable key names to achieve the effect of variable variables without the security risk.
Make sense? |
|||||||||||||||||
|
Use the built-in
|
|||||
|
It's not a good idea. If you are accessing a global variable you can use globals().
If you want to access a variable in the local scope you can use locals(), but you cannot assign values to the returned dict. A better solution is to use |
|||||||||
|
Whenever you want to use variable variables, it's probably better to use a dictionary. So instead of writing
you write
This way you won't accidentally overwrite previously existing variables (which is the security aspect) and you can have different "namespaces". |
|||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?