On eval and its substitution of globals

Leif K-Brooks eurleif at ecritters.biz
Wed Feb 23 15:38:54 EST 2005


Paddy wrote:
> I had to do as you suggest but I was thinking either it was a kludge,
>  and there should be a 'deep' substitution of globals, or that there
> was a good reason for it to work as it does and some magician would
> tell me.

If there was deep substitution of globals, how would functions imported 
from different modules behave? Consider this:

--- foo.py ---
from bar import func

data = 24

print func()
print eval('func()', globals(), locals())

--- bar.py ---
data = 42

def func():
     return data

--------------------------------------------------------------------

If globals were deeply substituted when using eval, the program would 
presumably print "42\n24", which would be far from intuitive. If you 
limit the deep substitution to functions in the same module, you're 
creating a confusing special case.



More information about the Python-list mailing list