
![]() | exec and eval may take dictionaries for the global and local namespace:
exec code in globals, locals eval(expr, globals, locals) |
![]() | Example:
a = 8; b = 9
d = {'a':1, 'b':2}
eval('a + b', d) # yields 3
and
from math import *
d['b'] = pi
eval('a+sin(b)', globals(), d) # yields 1
|
![]() | Creating such dictionaries can be handy |