When I execute external python code using the exec() method:
i = 0
exec("i = 99\nprint(i)")
print(i)
Output:
99
99
The code I'm executing changes the variable i in my original program. What alternative way of executing external python code can I use to hinder this? Consider that the code I'm executing is given to me as a string, and I have no control over it or its variable names.
Desired Output when executing the same code:
99
0
exec("yourcode", {}, {}). Or, wrap theexecto a function so that theexecwill not modify the global variable. BTW, It is not recommended to useexecin this manner.