does exec ignore the locals parameter?

cburns cburns4563 at gmail.com
Thu Jan 22 19:45:19 EST 2009


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.

Thanks,

Charlie



More information about the Python-list mailing list