Trouble accessing global vars

Miki Tebeka miki.tebeka at zoran.com
Sun Sep 5 12:21:48 EDT 2004


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 <miki.tebeka at zoran.com>
http://tebeka.spymac.net
The only difference between children and adults is the price of the toys



More information about the Python-list mailing list