Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > UnboundLocalError: local variable 'colorIndex' referenced

Reply
Thread Tools

UnboundLocalError: local variable 'colorIndex' referenced

 
 
silverburgh.meryl@gmail.com
Guest
Posts: n/a
 
      02-26-2006
Can you please tell me what is the meaning this error in general?

UnboundLocalError: local variable 'colorIndex' referenced before
assignment

In my python script,
I have a variable define and init to 0, like this
colorIndex = 0

and in one of my functions, I increment it by 1
def myFunc
colorIndex += 1

 
Reply With Quote
 
 
 
 
Rick Zantow
Guest
Posts: n/a
 
      02-26-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:1140987642.195734.187540
@t39g2000cwt.googlegroups.com:

> Can you please tell me what is the meaning this error in general?
>
> UnboundLocalError: local variable 'colorIndex' referenced before
> assignment
>
> In my python script,
> I have a variable define and init to 0, like this
> colorIndex = 0
>
> and in one of my functions, I increment it by 1
> def myFunc
> colorIndex += 1
>
>


It's a scoping issue. Within myFunc, if colorIndex receives a value
(that is, if you assign something to it, as you do here), Python
requires a local variable, one known within the scope of function. If
you had only *read* the variable (x = colorIndex, for instance), then
Python will first look for a local variable, and, finding none, will
then look for a global variable, which it would find in this case. The
net effect of all this is a common gotcha for new Python users: the
'colorIndex' that is assigned to within myFunc is *not* the same as the
one you assigned 0 to earlier; they just happen to share the same name.

You can get around this in various ways. One is to declare the variable
in myFunc, like this:
def myFunc
global colorIndex
colorIndex += 1
...

--
rzed
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-26-2006
(E-Mail Removed) schrieb:
> Can you please tell me what is the meaning this error in general?
>
> UnboundLocalError: local variable 'colorIndex' referenced before
> assignment
>
> In my python script,
> I have a variable define and init to 0, like this
> colorIndex = 0
>
> and in one of my functions, I increment it by 1
> def myFunc
> colorIndex += 1


It is alwasy a better idea to post whole scripts/working examples (even if working actaully means non-working). And this
is an example why: Using your old code, things worked. But inside a function, colorIndex isn't in the scope. You could
e.g. use a global-statement like this:


colorIndex = 0

def foo():
global colorIndex
colorIndex += 1

foo()


But that is not a very bright idea usually, as globals are difficult to debug.

Diez

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
local variable referenced before assignment johngilbrough Python 5 04-06-2010 07:48 AM
UnboundLocalError: local variable '_[1]' referenced before assignment Gabriel Rossetti Python 3 12-09-2009 02:48 PM
Error: UnboundLocalError: local variable 'PfFlag' referenced beforeassignment Wang, Harry Python 2 12-01-2007 06:13 PM
local variable referenced before assignment Pete Bartonly Python 10 10-25-2007 05:08 PM
HELP:UnboundLocalError: local variable '_nntp' referenced before assignment Peter Moscatt Python 2 03-19-2005 08:37 AM



Advertisments