473,796 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scope rules in nested functions

Hello,

I was writing a program which used some nested functions, and came
across a behaviour which I was unable to explain. I can summarise it
with the example below:

#!/usr/bin/env python

def evalfunction0(a ):
print "Call by eval - Success! arg =",a
def evalfunction3(a ):
def evalfunction1(a ):
string = "evalfunction0( a+1)"
eval(string)
def evalfunction2(a ):
string = "evalfunction1( a+1)"
eval(string)
# uncomment the next line to make the PREVIOUS line work!
# evalfunction1(-1)
string = "evalfunction2( a+1)"
eval(string)

def callfunction0(a ):
print "Function call - Success! arg =",a
def callfunction3(a ):
def callfunction1(a ):
callfunction0(a +1)
def callfunction2(a ):
callfunction1(a +1)
callfunction2(a +1)

callfunction3(0 )
evalfunction3(0 )

What I see (although I've only been able to test it in Python version
2.3 so far) is that the eval() call in evalfunction2, is unable to
resolve the symbol name evalfunction1 - even though it would be possible
to call that function directly. But it is even stranger to me that, if
evalfunction1() is called directly, then calling that function using
eval() from the same function also works.

I had previously assumed that the symbols available to eval() would be
the symbols available as literals, but it seems not. Is this a designed
feature, and if so would somebody be kind enough to describe why it
occurs?

More practically, if there are a large number of functions at the same
nesting level as evalfunction1() , is it possible for me to allow
evalfunction2() to access all of them without explicitly naming each one
as a literal?

Thanks,

Andrew

--
--- Andrew Collier ---- To reply by email, please use:
---- http://www.intensity.org.uk/ --- 'andrew {at} intensity.org.u k'
--
Have you lost your Marbles? http://www.marillion.com/
Jul 18 '05 #1
0 1175

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

Similar topics

33
3558
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
6
2570
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote: (http://www.python.org/search/hypermail/python-1993/0343.html) "This is because nested function definitions don't have access to the local variables of the surrounding block -- only to the globals of the
3
6479
by: Anonymous | last post by:
Is namespace the same thing as scope? While reading the book "Thinking in C++", I was under the impression that namespace is, well, a namespace--a feature to create a hiearchy for identifiers within the Global Namespace. And that, all identifiers within a given namespace, however deeply nested they are, each of them has the Global Scope. So, if namespace outer { int a = 1;
8
3382
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have "visibility."
18
1802
by: jslowery | last post by:
I am not completely knowledgable about the status of lexical scoping in Python, but it was my understanding that this was added in a long time ago around python2.1-python2.2 I am using python2.4 and the following code throws a "status variable" not found in the inner-most function, even when I try to "global" it. def collect(fields, reducer): def rule(record): status = True
78
4977
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only problem is that my little helper function doesn't work! It claims that a variable doesn't exist. If I move the variable declaration, it finds the variable, but can't change it. Declaring the variable global in the nested function doesn't work...
2
2294
by: James Brown | last post by:
All, Could anyone explain why the following is an error: void foo( struct FOO { int x; } f1 ) { } int main()
0
200
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 11:29:18 Cousson, Benoit, vous avez écrit : This is a language limitation. This is because nested scope is implemented for python function only since 2.3 allow late binding of free variables. the scope in class statment is not a closure, so there is only two possible scope in it : local and global. When "class C2(C1):" statment is interpreted, it is in the scope of class B for which a name C1 exists, but it...
0
154
by: Cousson, Benoit | last post by:
This is a language limitation. That was my understanding as well, but I think it is a pity to have that limitation. Don't you think that the same improvement that was done for method nested scope could be done as well for nested class? I can easily fix my current issue by doing the binding after the class declaration. My concern is more about the lack of symmetry of that approach; meaning that if both classes are in the global scope, one...
0
9531
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
10237
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 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...
1
10187
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,...
0
9055
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...
0
6795
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
5446
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
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
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
2
3735
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.