local variable not found
Alain Tesio
alain at onesite.org
Sat Apr 27 17:01:32 EDT 2002
Hi,
I have a problem trying to use code compiled on the fly,
python looks in the globals variables only.
Look at this example :
===
import codeop
def f():
x=[1,2,3]
eval(codeop.compile_command("y=x"))
print y
f()
==
It cannot find the variable y (I don't know its name at runtime):
Traceback (most recent call last):
File "/tmp/testlocal.py", line 8, in ?
f()
File "/tmp/testlocal.py", line 6, in f
print y
NameError: global name 'y' is not defined
But it works if I use print locals()["y"]
Is this an expected behaviour ? Why ??
Thanks
Alain
More information about the Python-list
mailing list