T
tedsuzman
-----
def f():
ret = 2
exec "ret += 10"
return ret
print f()
-----
The above prints '12', as expected. However,
------
def f():
ret = 2
exec "ret += 10" in globals(), locals()
return ret
print f()
------
prints '2'. According to http://docs.python.org/ref/exec.html, "In all
cases, if the optional parts are omitted, the code is executed in the
current scope." Don't globals() and locals() consist of the current
scope? Why aren't the two examples above equivalent?
def f():
ret = 2
exec "ret += 10"
return ret
print f()
-----
The above prints '12', as expected. However,
------
def f():
ret = 2
exec "ret += 10" in globals(), locals()
return ret
print f()
------
prints '2'. According to http://docs.python.org/ref/exec.html, "In all
cases, if the optional parts are omitted, the code is executed in the
current scope." Don't globals() and locals() consist of the current
scope? Why aren't the two examples above equivalent?