473,320 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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
2 3064
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike | last post by:
Hi This will not work, does any one know why? exec("/usr/usr/bin -C $U2 &"); Thanks
5
by: Thomas Brathans | last post by:
Hi, when I try to execute the obove mentioned to synchronise the servertime by a php-script, it doesn't work. Other shell-commands work fine. Executing ntpdate <server> directly in the shell...
5
by: Toby Donaldson | last post by:
Hi all, I'm designing an educational application that will run Python code and check the output against a pre-define answer. I want to use the "exec" statement to run the code, but I don't know...
1
by: Ted | last post by:
-------- def f(): ret = 2 exec "ret += 10" return ret print f() -------- The above example prints '12'. However, the following example prints
1
by: kurt.krueckeberg | last post by:
The second line of this script <?php // current directory echo getcwd() . "<br />"; print ( exec("ls *.*") ); ?> should display the names of the four files (it does in an ssh session) which...
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
1
by: frankrentef | last post by:
Newbie here.... I'm writing a Python program that has "def" functionality growing in leaps and bounds. I'm a newbie to Python so be detailed. Is there a way to create a py file with all the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.