does exec ignore the locals parameter?

Benjamin musiccomposition at gmail.com
Fri Jan 23 03:58:31 CET 2009


On Jan 22, 6:45 pm, cburns <cburns4... at gmail.com> wrote:
> In the code below, bar() seems to work, foo() seems broken.
>
> % python -V
> Python 2.6.1
>
> % cat exec1.py
>
> def foo(i) :
>         exec "i = i + 1" in locals(), globals()
>         print "i=%d" % i
>
> def bar(j) :
>         exec "j = j + 1"
>         print "j=%d" % j
>
> foo(0)
> bar(0)
>
> % python exec1.py
> i=0
> j=1
>
> What I really wanted to do was something like:
>
>         exec text in globals(), inspect.currentframe(1).f_locals but
> that didn't work either.

Trying to modify locals() in a function is undefined behavior.

>
> Thanks,
>
> Charlie




More information about the Python-list mailing list