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

xml0x1a at yahoo.com xml0x1a at yahoo.com
Mon Feb 19 22:52:43 EST 2007


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




More information about the Python-list mailing list