UnboundLocalError: local variable 'colorIndex' referenced

Discussion in 'Python' started by silverburgh.meryl@gmail.com, Feb 26, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Rick Zantow Guest

    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
    Rick Zantow, Feb 26, 2006
    #2
    1. Advertising

  3. 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
    Diez B. Roggisch, Feb 26, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Moscatt
    Replies:
    2
    Views:
    9,797
    Peter Moscatt
    Mar 19, 2005
  2. Wang, Harry
    Replies:
    2
    Views:
    305
  3. Gabriel Rossetti
    Replies:
    3
    Views:
    950
    Hrvoje Niksic
    Dec 9, 2009
  4. ch1zra
    Replies:
    9
    Views:
    3,087
    danieldelay
    Jun 8, 2010
  5. Replies:
    9
    Views:
    89
    John Gordon
    Jun 24, 2013
Loading...

Share This Page