exec in a nested function yields an error
Scott David Daniels
Scott.Daniels at Acm.Org
Tue Jan 13 13:13:47 EST 2009
TP wrote:
> ...
> def f():
> def f_nested():
> exec "a=2"
> print a
> f()
> ... What is the problem? Why?
What it wants is you to provide the "in context" portion of the
exec statement. I expect the reason it fails is that there is no
dictionary that is available as locals that encompasses the locals
of both f and f_nested, so you'll have to be explicit about what
you mean.
So, the code wants you to say something like:
def f():
def f_nested():
exec "a=2" in globals(), locals()
print a
return f_nested
f()
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list