How do I avoid this global SyntaxWarning?

Jacob Smullyan smulloni at smullyan.org
Sun Aug 26 05:03:15 CEST 2001


On a related note, you can get the same error with code like the following 
(which I recently generated via serendipitous stupidity):

def foo():
        global t
        t=whatever()
        # other confusing and ugly things happen, then
        # I accidentally redeclare t as global,
        # and get the same warning
        global t
        # etc

I have no problem with the redeclaration generating a warning,
but the warning message, while accurate (the line t="whatever"
does indeed precede the second global statement) doesn't make obvious
the real problem, which is that there are two global statements.

(Well,  who said that error messages are supposed to indicate what is 
really going on, anyway!  There are too many programmers around anyway
nowadays competing with me for recession jobs -- let's make them even 
more cryptic!)

Jacob Smullyan | smulloni at smullyan.org

-----------------------------------------
Siggy Brentrup <bsb at winnegan.de> writes:

> gradha at iname.com writes:
> 
> > Hi.
> > 
> > Promptly most of my scripts give SyntaxWarnings under Python 2.1.1, and the
> > warning is so strange to me, and doesn't explain anything or point in any
> > direction that I don't know exactly what to do. I've searched the
> > documentation for warning, syntax and global keywords, but I don't seem to
> > find what is making python angry. Here's the example:
> > 
> > #!/usr/bin/env python
> > 
> > expresion_include = "a string"
> > 
> > if __name__ == "__main__":
> >    global expresion_include
> >    print expresion_include
> > 
> > As said before, versions of Python 2.0 and earlier didn't show this warning.
> > What is it
> 
> SyntaxWarning: name 'expresion_include' is assigned to before global declaration
> 
> Exactly what it says, in line 3 you assigned to expresion_include,
> later in line 6 you declare it global.
> 
> > and how do I make it go away?
> 
> Remove the global statement, at module scope it doesn't make sense.
> 
> 
> Thanks
>   Siggy
> 
> -- 
> Siggy Brentrup - bsb at winnegan.de - http://oss.winnegan.de/
> ****** ceterum censeo javascriptum esse restrictam *******



More information about the Python-list mailing list