Consider the following example:
i=7
j=8
k=10
def test():
i=1
j=2
k=3
return dict((name,eval(name)) for name in ['i','j','k'])
It returns:
>>> test()
{'i': 7, 'k': 10, 'j': 8}
Why eval does not take into consideration the variables defined inside the function? From the documentation, optionally you can pass a globals and a locals dictionary. What does it means?Finally, how can I modify this small case to make it work?
globalbefore variable declaration inside a function but that's a bad idea, on the other hand, usingevalis usually a bad idea as well.