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

why is this not working? (nested scope question)

I have a problem understanding the scope of variable in nested
function. I think I got it nailed to the following example copied from
Learning Python 2nd edition page 205. Here is the code.

def f1() :
x=88
f2()
def f2() :
print 'x=',x
f1()

that returns an error saying that "NameError: global name 'x' is not
defined". I expected f2 to "see" the value of x defined in f1 since it
is nested at runtime. My reading of the book comforted me in this.

What am I missing? Shouldn't the E of the LEGB rule take care of that.
BTW, I am running this on python 2.3.

Thanks.

Sébastien.

Jul 26 '06 #1
7 1293
bi*************@ouranos.ca wrote:
I have a problem understanding the scope of variable in nested
function. I think I got it nailed to the following example copied from
Learning Python 2nd edition page 205. Here is the code.

def f1() :
x=88
f2()
def f2() :
print 'x=',x
f1()

that returns an error saying that "NameError: global name 'x' is not
defined". I expected f2 to "see" the value of x defined in f1 since it
is nested at runtime. My reading of the book comforted me in this.

What am I missing? Shouldn't the E of the LEGB rule take care of that.
BTW, I am running this on python 2.3.
For f1 to be seen as the enclosing scope of f2 f2 has to be /defined/ in f1:
>>def f1():
.... x = 88
.... def f2():
.... print "x =", x
.... f2()
....
>>f1()
x = 88

Just /calling/ a function inside another does not give the inner one access
to variables that are visible in the outer.

Peter
Jul 26 '06 #2
bi*************@ouranos.ca wrote:
I have a problem understanding the scope of variable in nested
function. I think I got it nailed to the following example copied from
Learning Python 2nd edition page 205. Here is the code.

def f1() :
x=88
f2()
def f2() :
print 'x=',x
f1()

that returns an error saying that "NameError: global name 'x' is not
defined". I expected f2 to "see" the value of x defined in f1 since it
is nested at runtime. My reading of the book comforted me in this.

What am I missing? Shouldn't the E of the LEGB rule take care of that.
There's a subtle difference between what you have:
>>def f1() :
.... x=88
.... f2()
>>def f2() :
.... print 'x=',x
>>f1()
[traceback]

and
>>def f1():
.... x = 88
.... def f2():
.... print 'x =',x
.... f2()
....
>>f1()
x = 88

The E in LEGB, as far as I understand it, involves to functions
whose *definitions* are nested within another function...not
those functions *called* within another function. Craziness
ensues with the next example:
>>def f1():
.... x = 99
.... def f2():
.... print 'x =',x
.... x = 77
.... f2()
....
>>f1()
x = 77
It makes sense...you just have to understand what it's doing. :)

-tkc

Jul 26 '06 #3

bi*************@ouranos.ca wrote:
[...]
def f1() :
x=88
f2()
def f2() :
print 'x=',x
f1()

that returns an error saying that "NameError: global name 'x' is not
defined". I expected f2 to "see" the value of x defined in f1 since it
is nested at runtime.
Ah, no, Python uses "static scoping". Google the term for more.
--
--Bryan

Jul 26 '06 #4

Thanks for the answers.

I do understand (and verified) that if I define f2 within f1, it works
as expected. But in the "learning pyton 2nd edition" at page 205 it is
said that "Programs are much simpler if you do not nest defs within
defs" (juste before the code mentioned in my initial message).

In a way, I though the local variables of f1 would in a way add to the
global variable of f2 (because f1 called f2) and that f2 would look in
the global variables when it could not find a variable locally
(following the LEGB rule).

Still the code I put is presented in the book and it does not work for
me. I googled for errata regarding that code but did not find any.

Sébastien.

Jul 26 '06 #5
bi*************@ouranos.ca wrote:
I do understand (and verified) that if I define f2 within f1, it works
as expected. But in the "learning pyton 2nd edition" at page 205 it is
said that "Programs are much simpler if you do not nest defs within
defs" (juste before the code mentioned in my initial message).
Actually, the code in the book is:

def f1():
x = 88
f2(x)

def f2(x):
print x

f1()

which makes all the difference in the world. Not to mention that this
particular section of the book is giving an example of how to write the
code *without* using nested functions.
Jul 27 '06 #6
Actually, the code in the book is:

def f1():
x = 88
f2(x)

def f2(x):
print x

f1()

which makes all the difference in the world. Not to mention that this
particular section of the book is giving an example of how to write the
code *without* using nested functions.
Ouch! You got me there, I did not copy the code properly. Now I feel
stupid. Thanks for the enlightment.

I think I am starting to get it.

Sébastien.

Jul 27 '06 #7
bi*************@ouranos.ca wrote:
Ouch! You got me there, I did not copy the code properly. Now I feel
stupid. Thanks for the enlightment.

I think I am starting to get it.
P.S. The point of the example was to show how nesting isn't necessary
much of the time. The authors wanted to show that it is okay to write a
call to f2 before f2 is even defined, as long as f2 is defined before
that call is actually executed, i.e. when f1() is called. Keeping the
two functions separate is cleaner than nesting, and passing parameters
is how you get around the local scope issue.
Jul 27 '06 #8

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

Similar topics

4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
12
by: Ali | last post by:
I have the following web page with a script in it. <html> <head> <title>Make Your Own Objects test</title> <script>
5
by: Neil Zanella | last post by:
Hello, I am curious as to why the designers of C did not include support for Pascal-like nested procedures. I guess that it's because nested procedures in C would not buy you much other than...
8
by: Sean Givan | last post by:
Hi. I'm new to Python, and downloaded a Windows copy a little while ago. I was doing some experiments with nested functions, and ran into something strange. This code: def outer(): val =...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
23
by: Steven D'Aprano | last post by:
I defined a nested function: def foo(): def bar(): return "bar" return "foo " + bar() which works. Knowing how Python loves namespaces, I thought I could do this:
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
1
by: hdogg | last post by:
Scope Woes - IF statement nested in WHILE statement -PHP I have an array $actuals_sum. <?php while(conditions) { if($i == '24) {
0
by: Cousson, Benoit | last post by:
Defining it as a nested class saves you one line The whole point of nested class is to avoid polluting the namespace with classes that are only used locally. So the argument about the elegance of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.