Having this code:
x = 10
exec('x += 5', {}, {'x': x})
print(x) # prints 10.
# This works of course
exec('x += 5')
print(x) # prints 15.
Why it does ignore my x += 5
expression? Is there something else I remove, by limiting globals/locals on exec
, so it does not allow to change variable?
P.S. Though it does work if I modify dictionary. Is it related with immutable types maybe?
Here's what is going on:
>>> x = 5
>>> loc = dict(x=x)
>>> exec('x += 5', {}, loc)
>>> print(loc)
{'x': 10}
>>> x
5