473,287 Members | 1,413 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,287 software developers and data experts.

exec(), execfile() and local-variable binding?

I'm puzzled by Python's behavior when binding local variables which
are introduced within exec() or execfile() statements. First, consider
this simple Python program:

# main.py
def f() :
x = 1
print "x:", x
f()

This just prints "x: 1" when run. Now, what happens if we move the
assignment into a separate file "assign.py", and attempt to read it in
with execfile()?

# assign.py
x = 1
print "x in assign.py:", x

# main_execfile.py
def f () :
execfile("assign.py")
print "x in main_execfile.py:", x
f()

So, you might assume that if execfile() worked like a C-style
#include, this would work fine. But it doesn't. Instead, when
"main_execfile.py" is compiled, the "x = 1" assignment in "assign.py"
is never seen, so the 'x' in the print statement is assumed to be a
global variable. Then at run-time, we get an error about 'x' being an
undefined global variable:

x in assign.py: 1
x in main_execfile.py:
Traceback (most recent call last):
File "main_execfile.py", line 4, in ?
f()
File "main_execfile.py", line 3, in f
print "x in main_execfile.py:", x
NameError: global name 'x' is not defined

Well, that's understandable. But what I find strange is that exec()
*doesn't* work in this way. Consider this version:

# main_exec.py
def f() :
exec("x = 1")
print "x:", x
f()

I would have thought that, just like the execfile() version, the "x =
1" string would not be interpreted at parse-time, so that the 'x' in
the "print" statement would again be taken to be a global variable.
But no, this version runs fine, producing the output "x: 1".

And this fancier version also runs:

# main_exec2.py
def f(v) :
exec(v + " = 1")
print "x:", x
print "Type 'x':"
f(input())

Here, if the user enters 'x', the assignment string "x = 1" is made
up, and the program runs as before.

So, why do these exec() versions work, when the execfile() one didn't?
Specifically, why aren't the 'x' variables in the "print" statements
taken as global variables? AFAIK, this should be a compile-time
decision, but the "x = 1" assignment strings can't have been
interpreted at that time.

Thanks for any help,

-- Jonathan
Jul 18 '05 #1
2 17315
jo*****@excite.com (Jonathan) wrote in message news:<c0**************************@posting.google. com>...
I'm puzzled by Python's behavior when binding local variables which
are introduced within exec() or execfile() statements. [...]


I now think I understand the gist of the problem. A Google group
search turned up some very similar past discussions. Essentially,
execfile() is a function, and so can't (reliably) change the
local-variable dictionary (locals()) which is passed into it. So any
local-variable settings which happen in the file which execfile()
executes are lost when it returns.

In contrast, exec() is a *statement*, not a function. Therefore, the
locals() dictionary passed into it is mutable. So variable assignments
in the string of an exec() *will* affect the local-variable dictionary
of the function which calls it. Python's usual static compile-time
variable binding can't cope with these potential dynamically-defined
variables. So therefore, if any exec() statement is seen in a function
body at compile-time, static binding is switched off, and a slower
form of run-time binding is used. This dynamically searches the
enclosing variable scopes (dictionaries) for variable definitions.

The run-time binding triggered by exec() explains a weird effect which
I stumbled on after I posted my original message. That is, the
execfile() version of the code *will* work if an exec() appears
anywhere before or after it in the f() function definition. This is
because run-time binding is now used for the whole function - even for
the execfile(). That's why I said above that execfile() can't
*reliably* change the calling function's local-variable dictionary -
it can if this run-time-binding mode is in effect.

Such sneaky insertion of a dummy exec() is not great form, though. The
recommended way to allow execfile() code to affect current variables
is to pass in an explicit context dictionary as an argument to
execfile(). Then you are free to use that dictionary as you see fit
(notably, using it for other exec()'s or execfile()'s). Another way to
implement more of a #include-type effect is to replace this:
execfile("blah.py")
with this:
exec open("blah.py").read()

However, once again, this solution will trigger the use of the slower
run-time binding for the whole enclosing function.

I trust the experts out there will comment if I misrepresented any of
these issues.

-- Jonathan
Jul 18 '05 #2
Quoth Jonathan:
[...]
Well, that's understandable. But what I find strange is that exec()
*doesn't* work in this way. Consider this version:

# main_exec.py
def f() :
exec("x = 1")
print "x:", x


Note that exec is a keyword, not a function; you might as well
write
exec 'x = 1'
I'm not just picking a nit here -- it is important for your
question that exec is a keyword, since this means it is possible
to determine at compile-time whether exec is used in the body of a
function. This is quite unlike calls to built-in functions such
as execfile(), which cannot in general be identified as such at
compile-time.

As you noted, in the presence of exec statements, the compiler
abandons the optimization by which LOAD_NAMEs are replaced with
LOAD_GLOBALs. This optimization normally speeds up variable
access by skipping a futile name lookup in the locals; abandoning
this optimization when there are exec statements makes your
example above work in the obvious and desired way.

--
Steven Taschuk st******@telusplanet.net
"I tried to be pleasant and accommodating, but my head
began to hurt from his banality." -- _Seven_ (1996)

Jul 18 '05 #3

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

Similar topics

3
by: | last post by:
Hi everybody, I'm sure there's a way to do this, but I can't find it. How can I execute another .py file from my first .py file without using an exec* command? They're both in the same...
2
by: Chris S. | last post by:
I'd like to dynamically execute multiple lines of indented code from within a script, but I can't seem to find a suitable function. Exec only works with unindented code, and execfile only works...
2
by: tedsuzman | last post by:
----- def f(): ret = 2 exec "ret += 10" return ret print f() ----- The above prints '12', as expected. However,
1
by: Andr? Roberge | last post by:
I have the following two files: #--testexec.py-- def exec_code(co): try: exec co except: print "error" #-- test.py--
1
by: Bo Jacobsen | last post by:
I have a number of files compiled to bytecode using py_compile.compile(). The .pyc files can be invoked by python directly ($python file.pyc), but "loading" them by execfile just throws an...
8
by: R. Bernstein | last post by:
In doing the extension to the python debugger which I have here: http://sourceforge.net/project/showfiles.php?group_id=61395&package_id=175827 I came across one little thing that it would be nice...
2
by: xml0x1a | last post by:
How do I use exec? 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)
5
by: Edward K Ream | last post by:
It looks like both exec and execfile are converting "\n" to an actual newline in docstrings! Start idle: Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win32
1
by: Fernando Perez | last post by:
Hi all, I'm finding the following behavior truly puzzling, but before I post a bug report on the site, I'd rather be corrected if I'm just missing somethin obvious. Consider the following...
5
by: George Sakkis | last post by:
I maintain a few configuration files in Python syntax (mainly nested dicts of ints and strings) and use execfile() to read them back to Python. This has been working great; it combines the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
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...
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: 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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.