473,326 Members | 2,023 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,326 software developers and data experts.

problem with namespaces using eval and exec

Hi,

has anyone an idea why the following code does not work.
s = """
def a(n):
return n*n
def b(t):
return a(t)
"""
ns = {}
exec(s, {}, ns)
eval("b(2)", ns, {})
executing this script raises an exception (NameError: global name 'a'
is not defined) in the last line.
Hope for your help.

May 16 '06 #1
3 1728
Le Mardi 16 Mai 2006 11:05, Jens a écrit*:
s = """
def a(n):
* return n*n
def b(t):
* return a(t)
"""
ns = {}
exec(s, {}, ns)
eval("b(2)", ns, {})

you passed an empty globals dictionnary to the eval func (the local one is not
in the scope of b defintion)
try this :

exec s // like exec s in globals(), locals()
eval("b(2)") // like , eval("b(2)", globals(), locals())

or if you to keep this dictionnary as a specific scope in your appplication :

exec s in ns
eval("b(2)", ns)

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
May 16 '06 #2
Jens wrote:
has anyone an idea why the following code does not work. s = """
def a(n):
return n*n def b(t):
return a(t)
""" ns = {}
exec(s, {}, ns)
Here you are providing a local namespace into which all toplevel names (a
and b) are inserted.
eval("b(2)", ns, {})


Here you provide a global (the former local) namespace containing an 'a'.
Function b(), however is carrying its global namespace with it and will
look for 'a' in that namespace and not the one you provide. This is similar
to

module x.py
def b(t): return a(t)

module y.py
import x
def a(t): return n * n
x.b(2) # error, will not find the function a() you just defined

Instead you could explicitly update b's global namespace:

global_ns = {}
local_ns = {}
exec s in global_ns, local_ns
global_ns["a"] = local_ns["a"]
print eval("b(2)", local_ns)

or (better, I think) model the situation of an ordinary module where
globals() and locals() are identical:

global_ns = {}
exec s in global_ns
print eval("b(2)", global_ns)

Peter
May 16 '06 #3
Thanks a lot, that works for me.

May 17 '06 #4

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

Similar topics

1
by: Rasjid Wilcox | last post by:
Hi, I found references on Google to a discussion a little while ago about using RestrictedPython instead of rexec and Bastion. But I've had trouble finding documentation. Below is my test...
47
by: Andrey Tatarinov | last post by:
Hi. It would be great to be able to reverse usage/definition parts in haskell-way with "where" keyword. Since Python 3 would miss lambda, that would be extremly useful for creating readable...
8
by: Filip Dreger | last post by:
Each function has a func_code property that is suposed to contain the pure bytecode of the function. All the context (including reference to relevant namespaces) is stored in different fields of...
8
by: Philippe C. Martin | last post by:
Hi, I'm getting pretty desperate here: The code below crashes on the last line (but works from a shell). The class 'BC' exists and the loop on self.__BC_EXEC_LIST passes fine. It's got to...
1
by: Bob Tinsman | last post by:
I've been interested in E4X because my company has an XML schema that we usually manipulate through a Java mapping generated by Castor, which I think is fairly tedious, and which means you have to...
5
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
0
by: Justus Schwabedal | last post by:
Dear python users! I try to setted up compile-free parallelism using the exec command. However I had some problems with namespaces which I find mysterious although I managed to work around. But...
4
by: grepla | last post by:
I have a simple line of code that requires the following inputs - an input file, output file and a SQL expression. the code needs to be run with several different SQL expressions to produce...
6
by: dave.g1234 | last post by:
Suppose I have a function in module X that calls eval e.g, X.py _______ Def foo(bar): Eval(bar) _______ Now eval will be called using the default eval(bar,globals(),locals()) and globals...
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...
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: 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: 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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.