I need to execute some dynamically generated code in Python and ensure that this code will have access to some custom global values, for example 'bar'. Based on python documentation i assembled the following test code:
def foo() : print( globals() )
exec "foo()" in dict( globals(), bar = 1 )
According to documentation, calling foo() from exec with bar added to scope will list bar among globals() inside foo() body. But the actual output is:
{'__builtins__': <module '__builtin__' (built-in)>, '__file__': 'C:\\Users\\Eye\
\Documents\\first.py', '__package__': None, '__name__': '__main__', 'foo': <func
tion foo at 0x01DD40F0>, '__doc__': None}
No bar here :(. How to make bar accessible from within code executed in exec?
(globals())and print that.