471,862 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,862 software developers and data experts.

exec "statement" VS. exec "statement" in globals(), locals()

-----
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?

Jul 18 '05 #1
2 4034
From Python Manual:

execfile(file[, globals[, locals]])
This function is similar to the exec statement, but parses a file instead
of a string. It is different from the import statement in that it does not
use the module administration -- it reads the file unconditionally and does
not create a new module.2.2
The arguments are a file name and two optional dictionaries. The file is
parsed and evaluated as a sequence of Python statements (similarly to a
module) using the globals and locals dictionaries as global and local
namespace. If the locals dictionary is omitted it defaults to the globals
dictionary. If both dictionaries are omitted, the expression is executed in
the environment where execfile() is called. The return value is None.

Warning: The default locals act as described for function locals() below:
modifications to the default locals dictionary should not be attempted. Pass
an explicit locals dictionary if you need to see effects of the code on
locals after function execfile() returns. execfile() cannot be used reliably
to modify a function's locals.

I think the Warning explains it.

You can do:

def f():
l={'ret':2}
exec("ret += 10", globals(), l)
return l['ret']
print f()

12

"tedsuzman" <te*******@yahoo.com> wrote in message
news:ma*************************************@pytho n.org...
-----
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?

Jul 18 '05 #2
Hi !
In the syntax 2, because "ret=2", "ret" is local to f()


Try :

chaine="""global ret
ret=ret+10"""

def f():
global ret
ret = 2
exec chaine in globals(), locals() #now, idem exec(chaine)
return ret

print f() #==> 12


Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Toby Donaldson | last post: by
28 posts views Thread by Steven Bethard | last post: by
5 posts views Thread by TPJ | last post: by
reply views Thread by Steven Bethard | last post: by
4 posts views Thread by =?ISO-8859-1?Q?Gregory_Pi=F1ero?= | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.