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

namespaces in eval(): spot error in 3 line program

I'm an old time python user, but just got bitten by namespaces in eval.
If this is an old discussion somewhere, feel free to point me there.

Based on the documentation, I would have expected the following to
work:

def foo(k): print k; print a

ns = {'a':1, 'foo': foo}
eval('foo(2)', ns)

But it does not, complete session:

[harri@labsdevgrid1 ~]$ python
Python 2.4 (#2, Feb 13 2005, 22:08:03)
[GCC 3.4.3 (Mandrakelinux 10.1 3.4.3-3mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
def foo(k): print k; print a .... ns = {'a':1, 'foo': foo}
eval('foo(2)', ns) 2
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 0, in ?
File "<stdin>", line 1, in foo
NameError: global name 'a' is not defined


huh? I'd almost be tempted to call this a bug?

Playing with locals() and globals() I see that this one works,
which I would not have expected to work:

def foo(k): print k; print ns['a']

ns = {'a':1, 'foo': foo}
eval('foo(2)', ns)

Prints out
2
1

Do functions carry their own pointer to global namespace,
which gets defined at function compile time, or what is
happening here?

-Harri

Jul 19 '05 #1
2 1382
In article <11**********************@g47g2000cwa.googlegroups .com>,
"pasa" <ha***********@trema.com> wrote:
I'm an old time python user, but just got bitten by namespaces in eval.
If this is an old discussion somewhere, feel free to point me there.

Based on the documentation, I would have expected the following to
work:

def foo(k): print k; print a

ns = {'a':1, 'foo': foo}
eval('foo(2)', ns)

But it does not, complete session:

[harri@labsdevgrid1 ~]$ python
Python 2.4 (#2, Feb 13 2005, 22:08:03)
[GCC 3.4.3 (Mandrakelinux 10.1 3.4.3-3mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
def foo(k): print k; print a ... ns = {'a':1, 'foo': foo}
eval('foo(2)', ns) 2
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 0, in ?
File "<stdin>", line 1, in foo
NameError: global name 'a' is not defined


huh? I'd almost be tempted to call this a bug?

Playing with locals() and globals() I see that this one works,
which I would not have expected to work:

def foo(k): print k; print ns['a']

ns = {'a':1, 'foo': foo}
eval('foo(2)', ns)

Prints out
2
1

Do functions carry their own pointer to global namespace,
which gets defined at function compile time, or what is
happening here?


Function definition time. Your code is more or less equivalent to:

# A.py:
def foo(k): print k; print a

# B.py:
from A import foo
a = 1
foo()

That will fail with a NameError just the same.

Just
Jul 19 '05 #2

"pasa" <ha***********@trema.com> wrote in message
Do functions carry their own pointer to global namespace,
which gets defined at function compile time, or what is
happening here?


To expand on Just's answer:

For lexical scoping -- resolution of globals in the definition context --
some reference to that context is required. The alternative of dynamic
scoping -- resolution of globals in the calling context -- has been tried
in other languages and found wanting. For instance, if you call a function
in the math module that uses pi, sin, or cos, one almost certainly wants it
to use the object defined and bound to pi, sin, or cos in the math module.
That happens automatically with lexical scoping. With dynamic scoping, one
would have to do 'from math import *' first. And so on for everything
else.

Terry J. Reedy

Jul 19 '05 #3

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

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
7
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed...
0
by: Michelle Keys | last post by:
Subject: DataBinder.Eval Error! Server Error in '/MSPOS' Application. ------------------------------------------------------------------------ -------- DataBinder.Eval:...
15
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
8
by: rdrink | last post by:
n.n.h. (noob needs help) Ok, I've been beating my head against this for a day... time to ask others. To explain things as simply as possible: I am trying to use eval() to evaluate some simple...
4
by: Jm lists | last post by:
Hello members, I want to know does the "eval" in python have the same features as in Perl (capture errors)? For example,in perl I can wrote: $re = eval { 1 / 0 }; Though 1/0 is a fatal...
0
by: J. Clifford Dyer | last post by:
On Tue, 2007-12-11 at 16:55 -0800, katie smith wrote: Katie, First, please provide a useful subject heading when posting to the list. It makes everyone's life easier when searching the...
5
by: Simon | last post by:
I have problem with namespaces. I have a program that consumes the web service and has for instance names space nsProgram. In this program I have defined several classes that I use for storing and...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
jinu1996
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...
0
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...
0
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,...
0
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...

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.