On Sun, Nov 15, 2009 at 12:38 PM, Terry Reedy <tjre...@udel.edu> wrote: > Ezio Melotti wrote: >> >> Python currently accepts global statements at the top level: >> >> 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/. >> >> Any thoughts? > > I make the same suggestion a couple of years ago, either this list or Py3k > list, after newby reported 'problem' on python-list expecting module-level > global to do something. Guido rejected it on the basis that he wanted to > minimized differences between module code and function code.
That example should work because you could pass it to exec()/eval() with separate dicts for locals and globals: $ python3.0 Python 3.0 (py3k:67506, Dec 3 2008, 10:12:04) [GCC 4.0.1 (Apple Inc. build 5484)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> glo = {} >>> lo = {} >>> so = 'global x; x = 2' >>> co = compile(so, '', 'exec') >>> exec(co, glo, lo) >>> glo['x'] ['x'] >>> exec('x = 3', glo, lo) >>> glo['x'] 2 >>> lo['x'] 3 >>> -- --Guido van Rossum (python.org/~guido) _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com