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#5 3>", line 1, in -toplevel-
b.run()
File "<pyshell#5 1>", line 3, in run
counter += 1
UnboundLocalErr or: local variable 'counter' referenced before assignment
However, counter is not a local var, it's a global one. :-? Shouldn't this
work? 6 2105
On Sat, 04 Sep 2004 17:30:36 +0200, Fernando Rodríguez
<fe************ *******@fernand o-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#5 3>", line 1, in -toplevel- b.run() File "<pyshell#5 1>", line 3, in run counter += 1 UnboundLocalEr ror: 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.
--
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#5 3>", line 1, in -toplevel- b.run() File "<pyshell#5 1>", line 3, in run counter += 1 UnboundLocalErr or: 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
On Sat, 04 Sep 2004 17:30:36 +0200, Fernando Rodríguez
<fe************ *******@fernand o-rodriguez.com> declaimed the following
in comp.lang.pytho n: counter = 0
class Blah(object): def run(self):
global counter
counter += 1
b = Blah() b.run()
<snip>
UnboundLocalErr or: 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.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG < wu******@dm.net | Bestiaria Support Staff < =============== =============== =============== =============== == < Home Page: <http://www.dm.net/~wulfraed/> < Overflow Page: <http://wlfraed.home.ne tcom.com/> <
Fernando Rodríguez <fe************ *******@fernand o-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#5 3>", line 1, in -toplevel- b.run() File "<pyshell#5 1>", line 3, in run counter += 1 UnboundLocalErr or: 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.ne t>
Hello Fernando, counter = 0
class Blah(object): def run(self): counter += 1
b = Blah() b.run()
Traceback (most recent call last): File "<pyshell#5 3>", line 1, in -toplevel- b.run() File "<pyshell#5 1>", line 3, in run counter += 1 UnboundLocalErr or: 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*********@zo ran.com> http://tebeka.spymac.net
The only difference between children and adults is the price of the toys
Troels Therkelsen <t_**********@h otmail.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 $name;
|
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
|
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):
return a + b
|
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 code:
#def put_file(file_id, delete=False):
# """ Function to put the file on the FTP Server
# """
# print " FTP for this file started"
|
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 window open, if I click
the link from the email again I get the desired page. The results are the
same if the url is copied and pasted into the brower but the 'go' must be
pressed twice.
It appears that the server is trying to resolve the url vars...
| |
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 space, I
tried to only use parameters and stack space for things which are truely
temporary. Instead of passing a pointer to a data structure which should
always be populated with data, I have the data structure declared as a
global variable and...
|
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 much everything. Any
advice appreciated... Here:
========================================================
#!/usr/bin/env jython
#imports
|
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 diagnostics;
use sigtrap;
|
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*
program wide globals. There are only module wide globals. Also, the
"global isglobal" is absolutely meaningless as anything declared there
is a (module level) global by definition.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |