473,473 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Global variables?

Hi,

I am having problems getting my script to recognize global variables and
each time I run the script I always get this warning:

SyntaxWarning: name 'getdata' is assigned to before global declaration

The structure of my script is below:
================================================== =========================
import urllib
import sys
global myvariable
myvariable = 0

class MyHTMLParser(HTMLParser):

def handle_starttag(self, tag, attrs):

'Do some stuff here and reassign a value to myvariable'
def handle_data(self, data):

if myvariable == 1:
'Do some more stuff here'

if __name__ == "__main__":
try:
file = sys.argv[1]
fd = open(file)
except IOError, detail:
print "Couldn't open file '%s' for reading!"%file, detail[1]
sys.exit(1)
except IndexError:
print "No filename given!"
sys.exit(2)

my_parser=MyHTMLParser() # Instantiate my Parser
my_parser.feed(fd.read()) # Feed it
my_parser.close() # Close the parser.
fd.close() # Close the file.
sys.exit(0)
================================================== =========================

It appears to totally ignore the fact that I have specified global
myvariable. if i do not specify global at all I get an error stating:

UnboundLocalError: local variable 'myvariable' referenced before assignment

What am I doing wrong?? (Im a Python newbie so be gentle!)

Thanks

Rigga
Jul 18 '05 #1
3 6966
"RiGGa" <ri***@hasnomail.com> wrote in message
news:9V*********************@stones.force9.net...
Hi,

I am having problems getting my script to recognize global variables and
each time I run the script I always get this warning:

SyntaxWarning: name 'getdata' is assigned to before global declaration

The structure of my script is below:
================================================== ========================= import urllib
import sys
global myvariable
This is in the wrong place -- you need this in the function where you are
referencing the global name (see below).
myvariable = 0

class MyHTMLParser(HTMLParser):

def handle_starttag(self, tag, attrs):

'Do some stuff here and reassign a value to myvariable'
def handle_data(self, data):
global myvariable
if myvariable == 1:
'Do some more stuff here'

--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #2
RiGGa wrote:
I am having problems getting my script to recognize global variables ...

global myvariable
myvariable = 0

class MyHTMLParser(HTMLParser):
def handle_data(self, data):
if myvariable == 1:
'Do some more stuff here'

if __name__ == "__main__": [snip code]
What am I doing wrong?? (Im a Python newbie so be gentle!)


"global" is only meaningful *inside* a function, not at
the module level where you have it. In fact, you have to
specify it in *each* function in which you intend to modify
the global variable.

Basically, if you use a variable name in a function and you
haven't said it's global, Python assumes it's local *if* you
are writing to it (or "rebinding the name", which is more
correct and quite different in some cases, but basically the
same thing for simple variables as you are using above).

For more, and for many other things you should read about,
see the FAQ, especially the following entry:
http://www.python.org/doc/faq/progra...-in-a-function

(That's all one line, in case it gets split up.)

-Peter
Jul 18 '05 #3
Russell Blau wrote:
"RiGGa" <ri***@hasnomail.com> wrote in message
news:9V*********************@stones.force9.net...
Hi,

I am having problems getting my script to recognize global variables and
each time I run the script I always get this warning:

SyntaxWarning: name 'getdata' is assigned to before global declaration

The structure of my script is below:

================================================== =========================
import urllib
import sys
global myvariable


This is in the wrong place -- you need this in the function where you are
referencing the global name (see below).
myvariable = 0

class MyHTMLParser(HTMLParser):

def handle_starttag(self, tag, attrs):

'Do some stuff here and reassign a value to myvariable'
def handle_data(self, data):


global myvariable

if myvariable == 1:
'Do some more stuff here'


Thank you!!!
Jul 18 '05 #4

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

Similar topics

1
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of...
10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
5
by: Richard A. DeVenezia | last post by:
Dear Experts: Suppose I have global variables: x1, x2, x3 and I have a function that needs to assign a value to a new global variable x4 something like function foo () { count = 0;
2
by: Patient Guy | last post by:
I have a library of functions representing a filesystem interface (essentially a file selection interface, to be used in opening/reading/writing/closing files). Heavily scripted HTML document...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
10
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
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...
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...
0
tracyyun
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...
0
agi2029
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,...
1
isladogs
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.