Subclassed dict as globals

Evan Simpson evan at tokenexchange.com
Wed Jan 26 18:38:37 CET 2005


In Python 2.4 the following works:

 >>> class G(dict):
...   def __getitem__(self, k):
...     return 'K' + k
...
 >>> g = G()
 >>> exec 'print x, y, z' in g
Kx Ky Kz
 >>>

...while in Python 2.3 it fails with NameError: name 'x' is not defined. 
  Is this an "accidental feature", or can I count on this working in 
future versions of Python?  For that matter, is there a way to do this 
(intercept global variable accesses) in Python 2.3?

Cheers,

Evan @ 4-am




More information about the Python-list mailing list