473,383 Members | 1,792 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,383 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 5007
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brad Clements | last post by:
I was going to file this as a bug in the tracker, but maybe it's not really a bug. Poor Python code does what I consider to be unexpected. What's your opinion? With Python 2.3.2 (but also...
6
by: Alex Gittens | last post by:
I'm trying to define a function that prints fields of given widths with specified alignments; to do so, I wrote some helper functions nested inside of the print function itself. I'm getting an...
15
by: Paddy | last post by:
Hi, I am trying to work out why I get UnboundLocalError when accessing an int from a function where the int is at the global scope, without explicitly declaring it as global but not when accessing...
9
by: Camellia | last post by:
hi all why it generates an "UnboundLocalError" when I do the following: <code> .... def main(): number = number() number_user = user_guess() while number_user != number:
2
by: Matthew Franz | last post by:
I'm probably fundamentally misunderstanding the way the interpreter works with regard to scope, but is this the intended behavior... franz-macbook:~ mdfranz$ python unboundlocal.py ('Darwin',...
2
by: Wang, Harry | last post by:
$$ TestCase ID : 001 Step : deleteDvc,206268 Result Eval type : XmlChk Step : deleteDvc,206269 Result Eval type : XmlChk Traceback (most recent call last): File "C:\UDR2\UDRxmlGateway.py", line...
8
by: defn noob | last post by:
isPrime works when just calling a nbr but not when iterating on a list, why? adding x=1 makes it work though but why do I have to add it? Is there a cleaner way to do it? def isPrime(nbr):...
1
by: nongnaeja | last post by:
I don't know ,why it's error This my code from PIL import Image def ch_red(tmp): R = tmp G = tmp B = tmp
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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...

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.