Tags: andeach, global, programming, python, recognize, run, script, time, variables, warningsyntaxwarning

Global variables?

On Programmer » Python

5,454 words with 3 Comments; publish: Wed, 26 Dec 2007 23:33:00 GMT; (20093.75, « »)

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

All Comments

Leave a comment...

  • 3 Comments
    • "RiGGa" <rigga.python.questionfor.info.hasnomail.com> wrote in message

      news:9VECc.21042$NK4.3479907.python.questionfor.info.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.

      #1; Wed, 26 Dec 2007 23:35:00 GMT
    • 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/progr...e-in-a-function

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

      -Peter

      #2; Wed, 26 Dec 2007 23:36:00 GMT
    • Russell Blau wrote:

      > "RiGGa" <rigga.python.questionfor.info.hasnomail.com> wrote in message

      > news:9VECc.21042$NK4.3479907.python.questionfor.info.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!!!

      #3; Wed, 26 Dec 2007 23:37:00 GMT