I am trying to find a good and secure way to call a variable using string substitution in order to set its value. This is a more best-practices and I am looking for the best possible way to do this.
First of all, using dictionary or similar structure is not what I want. I wish to set the value of a variable!
Example;
def test_my_var(name_param, val):
# Set initial values for each variable
my_var_1 = -1
my_var_2 = -1
my_var_3 = -1
...
my_var_n = -1
# Now I will update the value of mth variable
my_var_%{name_param}s = val # Of course this will not work!!
# Now print the new value of this variable
print my_var_%{name_param}s
I am looking for a practical way to accomplish this. Things that are tested and do not work are:
I do not want to keep my vars in dict or similar structure. They are local variables inside a function and they should stay as they are.
Using exec is not something I prefer since it will execute any command that is sent! So below is not a good way to go and is not a valid way.
def test_case(index, val):
var_1 = -1
var_2 = -1
exec("var_%s" = %s % (index, val))
print eval("var_%s" % index)
Tested ways of using locals()
and in setting the value, but fail to do so
def test_case(index, val):
val_1 = -1
val_2 = -1
print locals()["val_%s" % index]
locals()["val_%s" % index] = val
print val_1, val_2 # That will not update the calue of the variable `val_1` or any other variable
So I am looking for a smart way to change the value of a variable where variable name will be expressed as a string substitution.