By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,086 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,086 IT Pros & Developers. It's quick & easy.

exec "def.." in globals(), locals() does not work

P: n/a
How do I use exec?
>python -V
Python 2.4.3

----
from math import *
G = 1
def d():
L = 1
exec "def f(x): return L + log(G) " in globals(), locals()
f(1)
----

How do I use exec() such that:
1. A function defined in exec is available to the local scope (after
exec returns)
2. The defined function f has access to globals (G and log(x) from
math)
3. The defined function f has access to locals (L)

So far I have only been able to get 2 out of the 3 requirements.
It seems that exec "..." in locals(), globals() only uses the first
listed scope.

Bottomline:
exec "..." in globals(), locals(), gets me 1. and 3.
exec "..." in locals(), globals() gets me 1. and 2.
exec "..." in hand-merged copy of the globals and locals dictionaries
gets me 2. and 3.

How do I get 1. 2. and 3.?

Thanks in advance

Feb 20 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Feb 19, 10:52 pm, xml0...@yahoo.com wrote:
How do I use exec?
Before you ask this question, the one you should have an answer for is
"why do I (think I) have to use exec ?". At least for the example you
gave, you don't; Python supports local functions and nested scopes,
with no need for exec:

from math import *
G = 1

def d():
L = 1
def f(x):
return L + log(G)
return f(1)

>python -V
Python 2.4.3

----
from math import *
G = 1
def d():
L = 1
exec "def f(x): return L + log(G) " in globals(), locals()
f(1)
----

How do I use exec() such that:
1. A function defined in exec is available to the local scope (after
exec returns)
2. The defined function f has access to globals (G and log(x) from
math)
3. The defined function f has access to locals (L)

So far I have only been able to get 2 out of the 3 requirements.
It seems that exec "..." in locals(), globals() only uses the first
listed scope.

Bottomline:
exec "..." in globals(), locals(), gets me 1. and 3.
exec "..." in locals(), globals() gets me 1. and 2.
exec "..." in hand-merged copy of the globals and locals dictionaries
gets me 2. and 3.

How do I get 1. 2. and 3.?
L is local in d() only. As far as f() is concerned, L,G and log are
all globals, only x is local (which you don't use; is this a typo?).
If you insist on using exec (which, again, you have no reason to for
this example), take the union of d's globals and locals as f's
globals, and store f in d's locals():

from math import *
G = 1

def d():
L = 1
g = dict(globals())
g.update(locals())
exec "def f(x): return L + log(G) " in g, locals()
return f(1)
George

Feb 20 '07 #2

P: n/a
On Feb 19, 8:15 pm, "George Sakkis" <george.sak...@gmail.comwrote:
On Feb 19, 10:52 pm, xml0...@yahoo.com wrote:
How do I use exec?

Before you ask this question, the one you should have an answer for is
"why do I (think I) have to use exec ?". At least for the example you
gave, you don't; Python supports local functions and nested scopes,
with no need for exec:

from math import *
G = 1

def d():
L = 1
def f(x):
return L + log(G)
return f(1)
python -V
Python 2.4.3
----
from math import *
G = 1
def d():
L = 1
exec "def f(x): return L + log(G) " in globals(), locals()
f(1)
----
How do I use exec() such that:
1. A function defined in exec is available to the local scope (after
exec returns)
2. The defined function f has access to globals (G and log(x) from
math)
3. The defined function f has access to locals (L)
So far I have only been able to get 2 out of the 3 requirements.
It seems that exec "..." in locals(), globals() only uses the first
listed scope.
Bottomline:
exec "..." in globals(), locals(), gets me 1. and 3.
exec "..." in locals(), globals() gets me 1. and 2.
exec "..." in hand-merged copy of the globals and locals dictionaries
gets me 2. and 3.
How do I get 1. 2. and 3.?

L is local in d() only. As far as f() is concerned, L,G and log are
all globals, only x is local (which you don't use; is this a typo?).
If you insist on using exec (which, again, you have no reason to for
this example), take the union of d's globals and locals as f's
globals, and store f in d's locals():
Thanks! this works.
I tried to use g to merge locals and globals
But I didn't think of keeping the locals() so that f is visible in d.


Feb 20 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.