Why does Python's eval
not work inside a function? The same eval(compile(cmd))
code works in a global environment, but does not work inside the foo
function.
Simple example:
fn = '/tmp/tmp'
mode = 'single'
def foo(cmd, fn, mode):
eval(compile(cmd, fn, mode)) # <<< this does not work
print 'foo: cmd=', cmd
print 'foo: x=', x
cmd = "x = 1"
eval(compile(cmd, fn, mode)) # <<< this works
print 'global scope: cmd=', cmd
print 'global scope: x=', x
del(x)
foo('x = 9', fn, mode)
This is the output and error message:
global scope: cmd= x = 1
global scope: x= 1
foo: cmd= x = 9
foo: x=
Traceback (most recent call last):
File "ctest.py", line 20, in <module>
foo('x = 9', fn, mode)
File "ctest.py", line 12, in foo
print 'foo: x=', x
NameError: global name 'x' is not defined
In your function, the execution does work but x
ends up in locals()
, and then the print
statement tries to find x
in globals()
and so raises the NameError
.
fn = '/tmp/tmp'
mode = 'single'
def foo(cmd, fn, mode):
eval(compile(cmd, fn, mode))
print 'locals:', locals()
print 'foo: cmd=', cmd
print 'foo: x=', locals()['x']
cmd = "x = 1"
eval(compile(cmd, fn, mode))
print 'global scope: cmd=', cmd
print 'global scope: x=', x
del(x)
foo('x = 9', fn, mode)
Outputs:
global scope: cmd= x = 1
global scope: x= 1
locals: {'x': 9, 'cmd': 'x = 9', 'mode': 'single', 'fn': '/tmp/tmp'}
foo: cmd= x = 9
foo: x= 9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With