Global variables?

RiGGa rigga at
Thu Jun 24 20:17:59 CEST 2004


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__":
        file = sys.argv[1]
        fd = open(file)
    except IOError, detail:
        print "Couldn't open file '%s' for reading!"%file, detail[1]
    except IndexError:
        print "No filename given!"

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

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!)



More information about the Python-list mailing list