472,119 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

UnboundLocalError: local variable 'colorIndex' referenced

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

Feb 26 '06 #1
2 4935
si***************@gmail.com 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
Feb 26 '06 #2
si***************@gmail.com 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

Feb 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Brad Clements | last post: by
6 posts views Thread by Alex Gittens | last post: by
15 posts views Thread by Paddy | last post: by
9 posts views Thread by Camellia | last post: by
2 posts views Thread by Matthew Franz | last post: by
reply views Thread by leo001 | last post: by

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.