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()
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/)
Alas, this is one of the things that don't work yet. To set and get
Oh, well; I need to come up with a different example for subclassing a built-in type, then. Thanks! <puts on thinking cap>
You can use a similar example but not override __getitem__ / __setitem__; instead, add some new methods (e.g. merge() -- like update() but the existing key wins). --Guido van Rossum (home page: http://www.python.org/~guido/)
akuchlin@mems-exchange.org writes:
Oh, well; I need to come up with a different example for subclassing a built-in type, then. Thanks! <puts on thinking cap>
Andrew, You can take a look at that implementation of xml.dom.minidom.NodeList; for Python 2.2, the implementation goes like this: ---------------------------------------------------------------------- class NodeList(list): def item(self, index): if 0 <= index < len(self): return self[index] length = property(lambda self: len(self), doc="The number of nodes in the NodeList.") ---------------------------------------------------------------------- -Fred -- Fred L. Drake, Jr. <fdrake at acm.org> PythonLabs at Zope Corporation
On Tue, Oct 23, 2001 at 09:30:27PM -0400, Fred L. Drake, Jr. wrote:
You can take a look at that implementation of xml.dom.minidom.NodeList; for Python 2.2, the implementation goes like this: <vanishingly short implementation deleted>
Thanks; maybe something like that will do, and it sparks a stray thought: would it be worth making the file wrapper class in posixfile.py a subclass of file? --amk
akuchlin@mems-exchange.org writes:
Thanks; maybe something like that will do, and it sparks a stray thought: would it be worth making the file wrapper class in posixfile.py a subclass of file?
Given that the documentation for posixfile delares it to be (essentially) obsolete, I thnk the answer is "no". Code that is obsolete should not be touched as long as it continues to pass any relevant tests and there are no reported bugs. -Fred -- Fred L. Drake, Jr. <fdrake at acm.org> PythonLabs at Zope Corporation
participants (4)
-
akuchlin@mems-exchange.org -
Andrew Kuchling -
Fred L. Drake, Jr. -
Guido van Rossum