How do I accomplish variable variables in Python?
Here is an elaborative manual entry, for instance: Variable variables
I have heard this is a bad idea in general though, and it is a security hole in Python. Is that true?
How do I accomplish variable variables in Python?
Here is an elaborative manual entry, for instance: Variable variables
I have heard this is a bad idea in general though, and it is a security hole in Python. Is that true?
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
getattr
or store your variables in a dictionary and then access them by name.Instead of a dictionary you can also use
namedtuple
from the collections module, which makes access easier.For example:
The consensus is to use a dictionary for this - see the other answers. This is a good idea for most cases, however, there are many aspects arising from this:
That said, I've implemented a variable variables manager-class which provides some of the above ideas. It works for python 2 and 3.
You'd use the class like this:
If you wish to allow overwriting of variables with the same type only:
New coders sometimes write code like this:
The coder is then left with a pile of named variables, with a coding effort of O(m * n), where m is the number of named variables and n is the number of times that group of variables needs to be accessed (including creation). The more astute beginner observes that the only difference in each of those lines is a number that changes based on a rule, and decides to use a loop. However, they get stuck on how to dynamically create those variable names, and may try something like this:
They soon find that this does not work.
If the program requires arbitrary variable "names," a dictionary is the best choice, as explained in other answers. However, if you're simply trying to create many variables and you don't mind referring to them with a sequence of integers, you're probably looking for a
list
. This is particularly true if your data are homogeneous, such as daily temperature readings, weekly quiz scores, or a grid of graphical widgets.This can be assembled as follows:
This
list
can also be created in one line with a comprehension:The result in either case is a populated
list
, with the first element accessed withmy_calculator.buttons[0]
, the next withmy_calculator.buttons[1]
, and so on. The "base" variable name becomes the name of thelist
and the varying identifier is used to access it.Finally, don't forget other data structures, such as the
set
- this is similar to a dictionary, except that each "name" doesn't have a value attached to it. If you simply need a "bag" of objects, this can be a great choice. Instead of something like this:You will have this:
Use a
list
for a sequence of similar objects, aset
for an arbitrarily-ordered bag of objects, or adict
for a bag of names with associated values.I have tried both in python 3.7.3, you can use either globals() or vars()
Reference:
https://www.daniweb.com/programming/software-development/threads/111526/setting-a-string-as-a-variable-name#post548936
If you don't want to use any object, you can still use
setattr()
inside your current module: