I'm trying to create a custom python interpreter type application. I'm using exec statement (in Python 2.7.6) to execute given code but globals aren't working quite as expected. Could someone explain why this does not work:
def print_x():
print(x)
g = {'x': 10, 'print_x': print_x}
l = {}
exec('print_x()', g, l)
The result (whether the print_x function is in g or l), is an error:
NameError: global name 'x' is not defined
So, do the globals passed to exec not carry over to called functions?