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 2 3108
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mike |
last post by:
Hi
This will not work, does any one know why?
exec("/usr/usr/bin -C $U2 &");
Thanks
|
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...
|
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):
|
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
|
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.
| |
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:
|
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 named "program.py"
THNX
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |