How do I avoid this global SyntaxWarning?

Gerhard Häring gerhard.nospam at bigfoot.de
Sat Aug 25 12:24:32 EDT 2001


On Sat, 25 Aug 2001 17:35:20 +0200, gradha at iname.com <gradha at iname.com> wrote:
>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 and how do I make it go away?
>
>Forgot to say exact warning message:
>
>./test.py:0: SyntaxWarning: name 'expresion_include' is assigned to before
>  global declaration

Judging from the language reference, this looks like a bug to me: """
    The global statement is a declaration which holds for the entire
    current code block. It means that the listed identifiers are to be
    interpreted as globals."""

I only ever saw "global" used within functions, but the language ref. doesn't
include this restriction. IMHO when used at module-level, the global keyword
should simply be a no-operation. So that SyntaxWarning smells like a bug to me.

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    public key at homepage
public key fingerprint: DEC1 1D02 5743 1159 CD20  A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))



More information about the Python-list mailing list