Jun-14-2021, 03:41 PM
This is interesting.
def test(): a = 0 exec("print(dir())") test()
Output:['a']
exec is "inheriting" variables defined in the local scope, but these must not be the same variables. Changing the variable "inside" exec does not change the value "outside" exec.def test(): a = 0 exec("print(a);a=5;print(a)") print(a) test()
Output:0
5
0
This is similar to what happens when using fork. In the Unix/Linux world the forked process shares the same variables as the parent process. Changing "a" in the forked process changes "a" in the parent because a is the same variable, resides in the same memory. In Windows a brand new process is created for the fork and the parent process and forked process are separate. The forked process has the same variable names and initial values, but the forked variables are not the same variables that were defined in the parent process.