UnboundLocalError: local variable 'colorIndex' referenced

Diez B. Roggisch deets at nospam.web.de
Sun Feb 26 16:29:34 EST 2006


silverburgh.meryl at 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




More information about the Python-list mailing list