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@labsdevgr id1 ~]$ 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 2 1398
In article <11************ **********@g47g 2000cwa.googleg roups.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@labsdevgr id1 ~]$ 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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 a single
collections type, I should be proposing a new "namespaces" module
instead. Some of my reasons:
(1) Namespace is feeling less and less like a collection to me. Even
though it's still intended as a data-only structure, the use cases...
|
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 the script and
special attention was thrown at the fact the script used eval alot.
I don't use eval alot in my scripts - but I do use it - and since I
always out to learn more / improve my javascript skills, I'm curious why
something I thought...
|
by: Michelle Keys |
last post by:
Subject: DataBinder.Eval Error!
Server Error in '/MSPOS' Application.
------------------------------------------------------------------------
--------
DataBinder.Eval: 'System.Data.Common.DbDataRecord' does not contain a
property with the name REPORTTO.
Description: An unhandled exception occurred during the execution of the
|
by: manstey |
last post by:
Hi,
I have a text file called a.txt:
# comments
I read it using this:
|
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 equations, such as--
pow(AB,2)
pow(AB,2)+A
pow(A+B,2)
pow(A+B,2)+A
and so forth... for a variety of math operations (+,-,*) and a variety
| |
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 error but since it's in "eval" block so the perl
|
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 archives.
Second, the example you provided in your last post was converting a
string of the form "" to a list of integers. What is
happening here, is you are trying to convert a string of the form
|
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 handling internal
information. Than I have web service, that also uses the same classes (I
included the file as linked external resource). I included this web service
as web reference and used name wsWeb.
When I am trying to call the web...
|
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 will pull in anything in module X.
|
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: 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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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: 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...
|
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: 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...
| |
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
| |