473,573 Members | 2,866 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3086
On Feb 19, 10:52 pm, xml0...@yahoo.c om 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.c om 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
2593
by: Mike | last post by:
Hi This will not work, does any one know why? exec("/usr/usr/bin -C $U2 &"); Thanks
5
4327
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 works also well. So why doesn't it work? Thanks...
5
5773
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 how to get output from it. For instance, exec works like this: >>> code = """ for i in xrange(1, 5):
1
1682
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
2673
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 are located in public_html, but instead it displays just one filename, that of this script.
21
7815
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
8836
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 "def's" listed and just call them from the "program" py file? How would I do this? Say my "def" py file was named def.py and the program file was...
0
7784
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7705
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6424
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5601
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5294
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3734
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3739
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.