2 years ago
#55493

NKUwakwe
Why does locals()['var_name'] = value affect the actual local namespace?
def f():
s = 'foo'
loc = locals()
loc['s'] = 'bar'
print(s)
loc['z'] = 'baz'
print(locals())
f()
I understand that the locals function returns a copy of the local namespace. That's why the value of s doesn't get modified in the actual local namespace. If that's the case, then why does an additional key-value pair to loc affect the actual local namespace?
python
function
scope
namespaces
local
0 Answers
Your Answer