In article <a64d0cdb-13b8-438c-97c0-2f83def7c...@k1g2000prb.googlegroups.com>, Benjamin <musiccomposit...@gmail.com> wrote: > On Jan 22, 6:45 pm, cburns <cburns4...@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) > Trying to modify locals() in a function is undefined behavior.
BTW, the order of the parameters to exec are reversed; presumably what was intended is: exec "i = i + 1" in globals(), locals() That doesn't change the results in question, though, as is, after the call to foo(0), i (=1) exists as a global. -- Ned Deily, n...@acm.org -- http://mail.python.org/mailman/listinfo/python-list