I am trying to convert a string containing the assignment of a Python variable to an actual variable.
The following worked for me.
s = "VAR = [1,55]"
exec(s)
print(VAR)
But then, when it puts this code in a function, VAR is never defined.
def myFunction():
s = "VAR = [1,55]"
exec(s)
print(VAR)
myFunction()
I'm not sure what I am missing. Thanks in advance for your help!
Answers to a few questions ...
Error message: "NameError: name" VAR "not defined"
Usage: Python 3
Bret source
share