473,761 Members | 4,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

exec "statement" VS. exec "statement in globals(), locals()

Ted
--------
def f():
ret = 2
exec "ret += 10"
return ret

print f()
--------

The above example prints '12'. However, the following example prints
'2':

--------
def f():
ret = 2
exec "ret += 10" in globals(), locals()
return ret

print f()
--------

According to (http://docs.python.org/ref/exec.html), "In all cases, if
the optional parts are omitted, the code is executed in the current
scope." Don't globals() and locals() comprise the current scope? Why
isn't the output of each example the same?
Jul 18 '05 #1
1 1692
ro*******@gmail .com (Ted) wrote in
news:9a******** *************** ***@posting.goo gle.com:
According to (http://docs.python.org/ref/exec.html), "In all cases, if
the optional parts are omitted, the code is executed in the current
scope." Don't globals() and locals() comprise the current scope? Why
isn't the output of each example the same?


locals() is a copy of the current scope, not the original. You should never
expect updates to locals() to be reflected in the local variables of the
current scope.

Are you really sure you need to use exec at all? There are very few good
use cases where exec is the appropriate answer to a problem.
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5780
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):
2
4187
by: tedsuzman | last post by:
----- def f(): ret = 2 exec "ret += 10" return ret print f() ----- The above prints '12', as expected. However,
5
1541
by: Carlos Ribeiro | last post by:
Hello all, I have a module that retrieves code snippets from a database to be executed on the fly . As I make heavy use of accented characters inside strings (I'm in Brazil), it came to my attention that the exec statement does not implement PEP 263 encoding checking. It does not issue any warning when fed with arbitrary code that includes non-ASCII characters. I am not sure if this is: 1) a design feature;
2
36162
by: Scarab | last post by:
Hi,all, When I use following sql, an error occurs: insert into #tmprep EXECUTE proc_stat @start,@end There is a "select * from #tmp " in stored procedure proc_stat, and the error message is : Server: Msg 8164, Level 16, State 1, Procedure proc_stat, Line 42 An INSERT EXEC statement cannot be nested.
0
3745
by: Scarab | last post by:
Hi all, When I use following sql to get data in stored procedure, error occurs: insert into #tmp EXECUTE dbo.prc_1 @date1,@date1 Here is the source code, Thanks CREATE TABLE ( NULL , NULL ) ON
28
2947
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions! PEP: 359 Title: The "make" Statement Version: $Revision: 45366 $ Last-Modified: $Date: 2006-04-13 07:36:24 -0600 (Thu, 13 Apr 2006) $
5
2436
by: TPJ | last post by:
I have the following code: ----------------------------------- def f(): def g(): a = 'a' # marked line 1 exec 'a = "b"' in globals(), locals() print "g: a =", a
4
2148
by: =?ISO-8859-1?Q?Gregory_Pi=F1ero?= | last post by:
I'm curious why this code isn't working how I expect it to: import sys d=3 def func1(a,b,c): print a,b,c,d print sys.path exec "func1(1,2,3)" in {'func1':func1}
2
1654
by: Danny Shevitz | last post by:
Howdy, In my app I need to exec user text that defines a function. I want this function to unpickle an object. Pickle breaks because it is looking for the object definition that isn't in the calling namespace. I have mocked up a simple example that shows the problem. Run this first code (from create_pickle.py) to create the pickle. create_pickle.py: (run this first)
0
9377
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,...
0
9811
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
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...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
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
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.