Re: Why this throws an UnboundLocalError ?

Discussion in 'Python' started by Mark Lawrence, Jan 30, 2014.

  1. On 30/01/2014 22:46, Marc Aymerich wrote:
    > Dear all,
    >
    > I have a very simple module
    >
    > glic3@e4200:# cat globalstate.py
    > GLOBAL = 0
    >
    > def update():
    > GLOBAL += 1
    >
    >
    > however it doesn't work!!
    >
    > glic3@e4200:# python
    > Python 2.7.3 (default, Aug 1 2012, 05:14:39)
    > [GCC 4.6.3] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >>>> import globalstate
    >>>> globalstate.update()

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > File "globalstate.py", line 4, in update
    > GLOBAL += 1
    > UnboundLocalError: local variable 'GLOBAL' referenced before assignment
    >
    >
    > And I don't know why :(
    > Anyone ?
    >
    > Thanks!!
    >


    You must tell the update function that GLOBAL is global.

    --
    My fellow Pythonistas, ask not what our language can do for you, ask
    what you can do for our language.

    Mark Lawrence
    Mark Lawrence, 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,830
    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. Chris Angelico

    Re: Why this throws an UnboundLocalError ?

    Chris Angelico, Jan 30, 2014, in forum: Python
    Replies:
    0
    Views:
    61
    Chris Angelico
    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