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.

Author johnf
Recipients amaury.forgeotdarc, docs@python, johnf, mark.dickinson
Date 2012-06-18.17:45:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1340041502.91.0.343406168254.issue15099@psf.upfronthosting.co.za>
In-reply-to
Content
Thank you all for the quick and interesting responses!

Here is another example, this time showing a simple
    s
sometimes behaves like
    globals()['s']
and sometimes doesn't.

class Dict(dict):
    def __getitem__(self, key):
        if key == 's':
            return 'got s'
        return dict.__getitem__(self, key)

dct = Dict()
dct['the_dict'] = dct
print 0, id(dct)

source = """if 1:
    print '1', id(globals()), globals() is the_dict
    print ' ', globals()['s']
    print ' ', s
    def f():
        print '2', id(globals()), globals() is the_dict
        print ' ', globals()['s']
        print ' ', s
        print '3'
    f()"""

exec(source, dct)


Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:32:06) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
>>> import curiosity2
0 2459928
1 2459928 True
  got s
  got s
2 2459928 True
  got s
 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "curiosity2.py", line 22, in <module>
    exec(source, dct)
  File "<string>", line 10, in <module>
  File "<string>", line 8, in f
NameError: global name 's' is not defined
>>>
History
Date User Action Args
2012-06-18 17:45:03johnfsetrecipients: + johnf, amaury.forgeotdarc, mark.dickinson, docs@python
2012-06-18 17:45:02johnfsetmessageid: <1340041502.91.0.343406168254.issue15099@psf.upfronthosting.co.za>
2012-06-18 17:45:02johnflinkissue15099 messages
2012-06-18 17:45:01johnfcreate