3 ms·
This only works in global scope. It won't work in a function: def f(): a = 5 locals()['a'] = 6 print(a) # 5 Inside a function, acc
by adrian17 9y ago
This only works in global scope. It won't work in a function:
def f():
a = 5
locals()['a'] = 6
print(a) # 5
Inside a function, accesses/writes of locals use an internal array of local variables, to skip dicts. Same with constants. See:
https://github.com/python/cpython/blob/master/Python/ceval.c#L1190 https://github.com/python/cpython/blob/master/Python/ceval.c...