Message163110
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
>>> |
|
Date |
User |
Action |
Args |
2012-06-18 17:45:03 | johnf | set | recipients:
+ johnf, amaury.forgeotdarc, mark.dickinson, docs@python |
2012-06-18 17:45:02 | johnf | set | messageid: <1340041502.91.0.343406168254.issue15099@psf.upfronthosting.co.za> |
2012-06-18 17:45:02 | johnf | link | issue15099 messages |
2012-06-18 17:45:01 | johnf | create | |
|