473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trouble accessing global vars

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?
Jul 18 '05 #1
6 2081
On Sat, 04 Sep 2004 17:30:36 +0200, Fernando Rodríguez
<fe*******************@fernando-rodriguez.com> , 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.
--
Jul 18 '05 #2
In article <so********************************@4ax.com>, 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

Jul 18 '05 #3
On Sat, 04 Sep 2004 17:30:36 +0200, Fernando Rodríguez
<fe*******************@fernando-rodriguez.com> 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.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #4
Fernando Rodríguez <fe*******************@fernando-rodriguez.com> 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
<le***@telus.net>
Jul 18 '05 #5
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 <mi*********@zoran.com>
http://tebeka.spymac.net
The only difference between children and adults is the price of the toys
Jul 18 '05 #6
Troels Therkelsen <t_**********@hotmail.com> 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
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: lkrubner | last post by:
If I set a variable at the top of my code like this: $name = "Lawrence"; It is now a global variable. If, later on, in a function, I want to do this: function uppercaseName() { global...
8
by: ndsoumah | last post by:
hello guys I'm trying to get access to variables I put in a session variable from another page and it fails... here's the exact situation main file page1.php
13
by: David Rysdam | last post by:
Getting no answer yesterday, I've done some investigation and I obviously don't understand how python namespaces work. Here's a test program: #!/usr/bin/python b = 2 def sumWithGlobal(a):...
6
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following...
16
by: WaterBug | last post by:
When clicking on the following link from an email i.e - http://myserver/myapplication/myprogram.asp?urlvar1=some%20stuff&urlvar2=more%20stuff I get a server 500 error. With that same browser...
10
by: Kleenex | last post by:
Reason: I am working on an embedded project which has very limited memory (under 512 bytes, 60 or so of which is stack space), which translates into limited stack space. In order to save on stack...
1
by: Konstantinos Pachopoulos | last post by:
Hi, i had posted earlier for not being able to declare global vars. No i followed the suggestions and created a class, but still the vars do not seem to have a global scope. I have tried pretty...
4
by: pcaisse | last post by:
I'm having issues sharing global variables with Explorer. This problem probably has a simple answer (as with most newbie questions). The script.pl file: #!/usr/bin/perl -w use strict; use...
0
by: Gary Herron | last post by:
Jacob Davis wrote: Yuck, YUCK, YUCK! You are breaking *so* many good-programming-practices, I hardly know where to start. First off: A python global is not what you think. There are *no*...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.