[Python-Dev] global statements outside functions/methods should raise SyntaxError
Michael Foord
fuzzyman at voidspace.org.uk
Sun Nov 15 20:59:43 CET 2009
Ezio Melotti wrote:
> Python currently accepts global statements at the top level:
>>>> global foo
>>>>
>
> Beside being a meaningless operation, this might lead unexperienced
> user to make mistakes like:
>>>> foo = 5
>>>> global foo # make foo global
>>>> def func():
> ... print foo # access the global foo
> ...
>>>> func()
> 5
>>>> # it works!
>
> "global foo" should raise a SyntaxError, similarly to what already
> happens with "return":
>>>> return foo
> File "<stdin>", line 1
> SyntaxError: 'return' outside function
>
>
> I opened an issue on the tracker (http://bugs.python.org/issue7329)
> and Benjamin suggested to discuss this here.
> The test he mentioned is in test_global.py:
>
> def test4(self):
> prog_text_4 = """\
> global x
> x = 2
> """
> # this should work
> compile(prog_text_4, "<test string>", "exec")
>
> It just says that "it should work" but it doesn't say /why/.
Well, personally I think it would be a good thing if this raised an
exception during bytecode compilation - but it would fall under the
moratorium and have to wait a few years.
On the other hand it should be easy to get PyLint to include a checker
for this and they are very responsive to feature requests.
All the best,
Michael
>
> Any thoughts?
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk
>
--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog
More information about the Python-Dev
mailing list