[Python-Dev] Dictionary subclasses and exec (original) (raw)
Guido van Rossum guido@python.org
Tue, 23 Oct 2001 17:08:15 -0400
- Previous message: [Python-Dev] Dictionary subclasses and exec
- Next message: [Python-Dev] Dictionary subclasses and exec
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 foldkey (self, key): if not isinstance(key, str): raise TypeError, "All keys must be strings" return key.lower() def getitem (self, key): key = self.foldkey(key) return dictionary.getitem(self, key) def setitem (self, key, value): key = self.foldkey(key) dictionary.setitem(self, key, value) def delitem (self, key): key = self.foldkey(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/)
- Previous message: [Python-Dev] Dictionary subclasses and exec
- Next message: [Python-Dev] Dictionary subclasses and exec
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]