The following bit of code defines a dictionary that folds all its keys to lowercase before inserting them, but 'exec "Name = 1" in LowerCaseDict()' doesn't seem to call my __getitem__(), as the listed keys include 'Name'. Should this be expected to work?
--amk
class LowerCaseDict(dictionary): def _fold_key (self, key): if not isinstance(key, str): raise TypeError, "All keys must be strings" return key.lower()
def __getitem__ (self, key): key = self._fold_key(key) return dictionary.__getitem__(self, key)
def __setitem__ (self, key, value): key = self._fold_key(key) dictionary.__setitem__(self, key, value)
def __delitem__ (self, key): key = self._fold_key(key) dictionary.__delitem__(self, key, value)
d = LowerCaseDict() exec 'Name = 1' in d print d.keys()
Alas, this is one of the things that don't work yet. To set and get local variables, exec uses lower-level APIs (PyDict_SetItem and PyDict_GetItem) that you can't override. I've thought about what it would take to make this work as desired, but I haven't found a way yet that wouldn't (a) slow down the normal case, or (b) create subtle reference count bugs. --Guido van Rossum (home page: http://www.python.org/~guido/)