I want to read some input, which contains python assignment statements like this string:
"VARIABLE = 'something' + OTHER_VAR"
So I use one of these:
exec("VARIABLE = 'something' + OTHER_VAR")
exec("VARIABLE = 'something' + OTHER_VAR", globals(), locals())
I want to use this variable in other code, but after exec(...) it is not in current namespace. It is possible to get the variable value like this:
locals()['VARIABLE']
however, if I dont know the name of variable it is not solution for me.
So how to get that new variable into my namespace?
UPDATE: My data for exec are like this:
COMPBLOCK = StringLeft(MyPlatform.HierarchicalName,6) +"_IADI." + CMP + "." + BLOCK ;
SetCustomPropertyValue("DEVLM2",COMPBLOCK + ".DEVLM2",false);
SetCustomPropertyValue("CRIT",COMPBLOCK + ".CRIT",false);
SetCustomPropertyValue("UNACK",COMPBLOCK + ".UNACK",false);
SetCustomPropertyValue("ALMSTA_26",COMPBLOCK + ".ALMSTA#B26",false);
I defined functions SetCustomPropertyValue
and StringLeft
. I want to avoid some complicated translation of this script to python with all possible inputs. Exec()
seems to be very quick solution, but after reading this post - Modifying locals in python I am little bit stuck.