This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: global statements outside functions/methods should raise SyntaxError
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.2, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, ezio.melotti, ggenellina, rhettinger, terry.reedy
Priority: normal Keywords:

Created on 2009-11-15 15:10 by ezio.melotti, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg95299 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2009-11-15 15:10
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
msg95303 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-11-15 17:33
Please bring this up on Python-dev if you'd like to change it. The fact
that there's a test verifying that this can happen suggests there is
some reason.
msg95333 - (view) Author: Gabriel Genellina (ggenellina) Date: 2009-11-16 09:38
The compiler doesn't know how the code is going to be used apart from 
the "mode" parameter:

py> c=compile("x=1","","exec")
py> import dis
py> dis.dis(c)
  1           0 LOAD_CONST               0 (1)
              3 STORE_NAME               0 (x)
              6 LOAD_CONST               1 (None)
              9 RETURN_VALUE
py> c=compile("global x; x=1","","exec")
py> dis.dis(c)
  1           0 LOAD_CONST               0 (1)
              3 STORE_GLOBAL             0 (x)
              6 LOAD_CONST               1 (None)
              9 RETURN_VALUE

The generated code is different, and I may exec it at global or local 
scope, with different results. 

compile would require a new mode, different from "exec", to 
mean "compile this as a module at global scope; forbid global 
statements"

If not, this would become invalid:

def foo():
  c=compile("global x; x=1","","exec")
  exec c

since -at the compile phase- the code is indistinghishable from a 
module.

Also, since PEP3003 has been approved (moratorium), language changes 
like this will have to wait a few years.
msg95564 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2009-11-20 21:20
I once proposed this change, but became less supportive as I can to
appreciate have fewer rather than more special-case rules. With
Gabriel's explanation, I now oppose the idea and think this should be
closed.
msg95593 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-11-22 01:28
I concur.
History
Date User Action Args
2022-04-11 14:56:54adminsetgithub: 51578
2009-11-22 01:28:27rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg95593

resolution: rejected
2009-11-20 21:20:30terry.reedysetnosy: + terry.reedy
messages: + msg95564
2009-11-16 09:38:26ggenellinasetnosy: + ggenellina
messages: + msg95333
2009-11-15 17:33:40benjamin.petersonsetmessages: + msg95303
2009-11-15 15:10:25ezio.melotticreate