Trouble accessing global vars

  • Thread starter Fernando Rodríguez
  • Start date
F

Fernando Rodríguez

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?
 
G

Gary

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.
--
 
T

Troels Therkelsen

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
 
D

Dennis Lee Bieber

counter = 0

class Blah(object):
def run(self):

global counter
counter += 1

b = Blah()
b.run()
said:
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.

--
 
L

Lenard Lindstrom

Fernando Rodríguez said:
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
<[email protected]>
 
M

Miki Tebeka

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.
 
A

Alex Martelli

Troels Therkelsen said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top