Re: Why this throws an UnboundLocalError ?

Discussion in 'Python' started by Chris Angelico, Jan 30, 2014.

  1. On Fri, Jan 31, 2014 at 9:46 AM, Marc Aymerich <> wrote:
    > GLOBAL = 0
    >
    > def update():
    > GLOBAL += 1


    If you assign to a name, Python makes it local, unless you explicitly
    tell it that you want it to be global:

    def update():
    global GLOBAL
    GLOBAL += 1

    But be aware that the ALL_CAPS name conventionally means a constant.
    Since you're changing its value, it's not constant (wow! :) ), so
    using a name of GLOBAL is a bad idea. (Also, obviously, you want to
    name it appropriately to what you're doing, but I assume you called it
    this as part of cutting down your example. For which, by the way,
    thank you. You posted a complete example, and the full traceback, and
    the Python version and platform. That's everything that we need to
    help you - it's such a luxury!!)

    ChrisA
    Chris Angelico, Jan 30, 2014
    #1
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,833
    Smokey Grindel
    Dec 2, 2006
  2. Marc Aymerich

    Why this throws an UnboundLocalError ?

    Marc Aymerich, Jan 30, 2014, in forum: Python
    Replies:
    0
    Views:
    57
    Marc Aymerich
    Jan 30, 2014
  3. Mark Lawrence

    Re: Why this throws an UnboundLocalError ?

    Mark Lawrence, Jan 30, 2014, in forum: Python
    Replies:
    0
    Views:
    66
    Mark Lawrence
    Jan 30, 2014
  4. Ned Batchelder

    Re: Why this throws an UnboundLocalError ?

    Ned Batchelder, Jan 30, 2014, in forum: Python
    Replies:
    0
    Views:
    69
    Ned Batchelder
    Jan 30, 2014
  5. Marc Aymerich

    Re: Why this throws an UnboundLocalError ?

    Marc Aymerich, Jan 31, 2014, in forum: Python
    Replies:
    0
    Views:
    62
    Marc Aymerich
    Jan 31, 2014
Loading...

Share This Page