Trouble accessing global vars

Discussion in 'Python' started by Fernando Rodríguez, Sep 4, 2004.

  1. Hi,

    I haven't used Python in quite some time, and I'm bit puzzled by this:

    counter = 0

    class Blah(object):
    def run(self):
    counter += 1

    b = Blah()
    b.run()

    Traceback (most recent call last):
    File "<pyshell#53>", line 1, in -toplevel-
    b.run()
    File "<pyshell#51>", line 3, in run
    counter += 1
    UnboundLocalError: local variable 'counter' referenced before assignment

    However, counter is not a local var, it's a global one. :-? Shouldn't this
    work?
    Fernando Rodríguez, Sep 4, 2004
    #1
    1. Advertising

  2. Fernando Rodríguez

    Gary Guest

    On Sat, 04 Sep 2004 17:30:36 +0200, Fernando Rodríguez
    <> , created a minor stir
    when he wrote:

    >Hi,
    >
    >I haven't used Python in quite some time, and I'm bit puzzled by this:
    >
    >counter = 0
    >
    >class Blah(object):
    > def run(self):

    global counter <<----- try this here...
    > counter += 1
    >
    >b = Blah()
    >b.run()
    >
    >Traceback (most recent call last):
    > File "<pyshell#53>", line 1, in -toplevel-
    > b.run()
    > File "<pyshell#51>", line 3, in run
    > counter += 1
    >UnboundLocalError: local variable 'counter' referenced before assignment
    >
    >However, counter is not a local var, it's a global one. :-? Shouldn't this
    >work?


    Check out Byte of Python here, too:
    www.python.g2swaroop.net
    It's proven very helpful.
    --
    Gary, Sep 4, 2004
    #2
    1. Advertising

  3. In article <>, Fernando Rodríguez wrote:
    > Hi,
    >
    > I haven't used Python in quite some time, and I'm bit puzzled by this:
    >
    > counter = 0
    >
    > class Blah(object):
    > def run(self):
    > counter += 1
    >
    > b = Blah()
    > b.run()
    >
    > Traceback (most recent call last):
    > File "<pyshell#53>", line 1, in -toplevel-
    > b.run()
    > File "<pyshell#51>", line 3, in run
    > counter += 1
    > UnboundLocalError: local variable 'counter' referenced before assignment
    >
    > However, counter is not a local var, it's a global one. :-? Shouldn't this
    > work?


    If you want to modify a global variable from inside a function/method scope,
    you need explicitly tell Python that this is indeed your wish, by using the
    global keyword, like this:

    class Blah(object):
    def run(self):
    global counter
    counter += 1

    Hope this helps,

    Troels Therkelsen
    Troels Therkelsen, Sep 4, 2004
    #3
  4. On Sat, 04 Sep 2004 17:30:36 +0200, Fernando Rodríguez
    <> declaimed the following
    in comp.lang.python:

    >
    > counter = 0
    >
    > class Blah(object):
    > def run(self):


    global counter

    > counter += 1
    >
    > b = Blah()
    > b.run()
    >

    <snip>

    > UnboundLocalError: local variable 'counter' referenced before assignment
    >
    > However, counter is not a local var, it's a global one. :-? Shouldn't this
    > work?


    Undeclared globals can be READ from, but any assignment creates
    a local of that name. Since

    counter += 1

    is effectively

    counter = counter + 1

    the left-hand side, during parsing, flags counter as a local; then, at
    run time, it sees a right-hand side "counter" and objects that you
    haven't given it an initial value.

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Sep 4, 2004
    #4
  5. Fernando Rodríguez <> writes:

    > Hi,
    >
    > I haven't used Python in quite some time, and I'm bit puzzled by this:
    >
    > counter = 0
    >
    > class Blah(object):
    > def run(self):
    > counter += 1
    >
    > b = Blah()
    > b.run()
    >
    > Traceback (most recent call last):
    > File "<pyshell#53>", line 1, in -toplevel-
    > b.run()
    > File "<pyshell#51>", line 3, in run
    > counter += 1
    > UnboundLocalError: local variable 'counter' referenced before assignment
    >
    > However, counter is not a local var, it's a global one. :-? Shouldn't this
    > work?


    Name counter has to be declared global in method run:

    class Blah(object):
    def run(self):
    global counter
    counter += 1

    The augmented assignment statements such as counter += 1 bind a name to
    a value. Binding a name within a function block makes the variable local
    by default.

    Lenard Lindstrom
    <>
    Lenard Lindstrom, Sep 4, 2004
    #5
  6. Fernando Rodríguez

    Miki Tebeka Guest

    Hello Fernando,

    > counter = 0
    >
    > class Blah(object):
    > def run(self):
    > counter += 1
    >
    > b = Blah()
    > b.run()
    >
    > Traceback (most recent call last):
    > File "<pyshell#53>", line 1, in -toplevel-
    > b.run()
    > File "<pyshell#51>", line 3, in run
    > counter += 1
    > UnboundLocalError: local variable 'counter' referenced before assignment

    You need to declare it global using the "global" keyword.

    counter = 0

    class Blah(object):
    def run(self):
    counter += 1

    b = Blah()
    b.run()

    Bye.
    --
    ------------------------------------------------------------------------
    Miki Tebeka <>
    http://tebeka.spymac.net
    The only difference between children and adults is the price of the toys
    Miki Tebeka, Sep 5, 2004
    #6
  7. Troels Therkelsen <> wrote:
    ...
    > If you want to modify a global variable from inside a function/method scope,
    > you need explicitly tell Python that this is indeed your wish, by using the
    > global keyword, like this:


    _MODIFY_ (call a method that performs modification on a mutable object)
    would be no problem. (bind or) _REBIND_ a global name, that's the
    troublespot where 'global' is needed. The += operator, like any other
    assignment, REBINDS the name (even when the object is mutable, so the
    change is in-place, nevertheless the name-rebinding occurs, for
    uniformity AND since that must be determined by the compiler which can't
    rely on knowing the object type).


    Alex
    Alex Martelli, Sep 6, 2004
    #7
    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. Replies:
    1
    Views:
    382
  2. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    375
  3. Martin Drautzburg

    accessing module global vars by name

    Martin Drautzburg, Dec 20, 2004, in forum: Python
    Replies:
    2
    Views:
    258
    Peter Hansen
    Dec 20, 2004
  4. Linuxguy123
    Replies:
    7
    Views:
    655
    Paddy O'Loughlin
    Feb 20, 2009
  5. caccolangrifata
    Replies:
    18
    Views:
    378
    Chris Torek
    Jul 22, 2011
Loading...

Share This Page