Presumably you are doing exec(d['plusone'])
in some scope wherein x
is defined, otherwise you would get a NameError
.
It seems far better to put a callable into your dict
and explicitly pass it x
(and explicitly assign to x
):
from operator import add, sub
d={'plusone':lambda x: add(x,1), 'minusone':lambda x: sub(x,1)}
x = 2
x = d['plusone'](x)
x
Out[17]: 3