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

Question about nested scopes

Hello,

Can anyone explain why:
def make_inc(n): s = n
def inc(i):
s += i
return s
return inc
i = make_inc(3)
i(2)
Traceback (most recent call last):
File "<pyshell#36>", line 1, in -toplevel-
i(2)
File "<pyshell#34>", line 4, in inc
s += i
UnboundLocalError: local variable 's' referenced before assignment
But: def make_inc(n): s = [n]
def inc(i):
s[0] += i
return s[0]
return inc
i = make_inc(2)
i(2) 4 i(2)

6

Thanks.
Miki
Jul 18 '05 #1
3 1474

"Miki Tebeka" <te****@cs.bgu.ac.il> wrote in message
news:33**************************@posting.google.c om...
Hello,

Can anyone explain why:
def make_inc(n): s = n
def inc(i):
s += i
return s
return inc [error snipped]
But: def make_inc(n): s = [n]
def inc(i):
s[0] += i
return s[0]
return inc


For the same reason as
s=1
s is 1 True s+=1
s is 2

True

Assignment to from within a function creates a local variable. Modification
of a mutable does not.

--
Emile van Sebille
em***@fenx.com
Jul 18 '05 #2
Miki Tebeka wrote:
Hello,

Can anyone explain why:
def make_inc(n): s = n
def inc(i):
s += i
return s
return inc
i = make_inc(3)
i(2)
Traceback (most recent call last):
File "<pyshell#36>", line 1, in -toplevel-
i(2)
File "<pyshell#34>", line 4, in inc
s += i
UnboundLocalError: local variable 's' referenced before assignment


you cannot change name-bindings in outer scopes.
the compiler probably thinks because of 's+=i' that s is a
local variable to 'inc', the inner function. but upon execution
it notices that it is not initialized and raises the usual exception.

However,

But: def make_inc(n):

s = [n]
def inc(i):
s[0] += i
return s[0]
return inc


here you don't modify the name-binding (s references a list
all the time) but you mutate the list.

holger

Jul 18 '05 #3
Hello Emile,
Assignment to from within a function creates a local variable. Modification
of a mutable does not.

Ok, thanks.

Miki
Jul 18 '05 #4

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

Similar topics

3
by: Nils Grimsmo | last post by:
hi, i'm having some trouble nesting functions. consider the following: def h(): x = 1 def g(): print x # ok, x is taken from h g()
5
by: Dave Benjamin | last post by:
I ran into an odd little edge case while experimenting with functions that create classes on the fly (don't ask me why): >>> def f(x): ... class C(object): ... x = x ... print C.x ......
32
by: David | last post by:
Hi I'm trying to teach myself python and so far to good, but I'm having a bit of trouble getting a function to work the way I think it should work. Right now I'm taking a simple program I wrote in...
9
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
10
by: John Salerno | last post by:
Here's a sentence from Learning Python: "Names not assigned a value in the function definition are assumed to be enclosing scope locals (in an enclosing def), globals (in the enclosing module's...
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...
78
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...
0
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...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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
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...
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,...

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.