I have a python 3 function that takes a string of commands, say 'np.pi' and then tries to define a variable with that string. Then I try to return the variable, this however does not work.
input = np.pi astring = 'funcD(funcC(funcB(funcA(input))))' def function(astring): astring= 'variable = ' + astring exec(astring) return variable In: a = function(astring) Out: NameError: global name 'variable' is not defined Nothing seems to have happened. What I would like is to have the function return the output of the command in the string. The string contains several functions who have each other as input like below. I tried putting the exec after return without adding the variable = and then call the function with a = function(astring) but that did not work either. I believe I cant use eval because my string has functions in it.
execto assign it, justreturnitsevalinstead:return eval(astring).