Global variables?

R

RiGGa

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
 
R

Russell Blau

RiGGa said:
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
 
P

Peter Hansen

RiGGa said:
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/programming.html#how-do-you-set-a-global-variable-in-a-function

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

-Peter
 
R

RiGGa

Russell said:
===========================================================================

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


global myvariable
Thank you!!!
 

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
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top