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#3 6>", line 1, in -toplevel-
i(2)
File "<pyshell#3 4>", line 4, in inc
s += i
UnboundLocalErr or: 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 3 1490
"Miki Tebeka" <te****@cs.bgu. ac.il> wrote in message
news:33******** *************** ***@posting.goo gle.com... 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
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#3 6>", line 1, in -toplevel- i(2) File "<pyshell#3 4>", line 4, in inc s += i UnboundLocalErr or: 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
Hello Emile, Assignment to from within a function creates a local variable. Modification of a mutable does not.
Ok, thanks.
Miki This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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()
|
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
...
>>> f(5)
Traceback (most recent call last):
|
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 Fortran and
trying to do it in Python. I got it to work, but now I'm trying to
modularize it. My fortran program uses a subroutine and I was trying
to do the same thing in Python. But I'm still new so I'm having
trouble understanding what I'm...
|
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;
|
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 namespace) or built-in (in the predefined __builtin__ names
module Python provides."
I have trouble reading this sentence. First, I don't understand if the
word 'enclosing' is a verb or an adjective. The whole flow of the
sentence seems...
| |
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 = 10
def inner():
print val
|
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 in a
nested scope, on variables from the outer scope:
PythonWin 2.4.3 (#69, Mar 29 2006, 17:35:34) on win32.
Portions Copyright 1994-2004 Mark Hammond (mhammond@skippinet.com.au) -
|
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...
|
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...
|
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: 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: 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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |