The function:
def thefunction():
exec("x = 'text'")
return x
print(thefunction())
and it doesnt work... this is for python 3
you need to pass in your locals or it will only be local to that ...
def thefunction():
my_locals = locals()
exec("x = 'text'",globals(),my_locals)
return my_locals['x']
print(thefunction())
you can make it a little safer by sending in an isolated environment... but the bottom line is that its not safe to exec untrusted input
NameError: name 'x' is not defined, in Python 3.10.8. It is valid to use globals() as the third argument, which writes the result to the global variable, then retrieves x from the global variable and returns it.x from the global variable and retest it? The local variable of the function is now implemented using an array. The document of exec never guarantees that it can work properly in the function...
execdocumentation?