[Python-Dev] PEP 455: TransformDict (original) (raw)
Steven D'Aprano steve at pearwood.info
Tue Oct 8 05:13:30 CEST 2013
- Previous message: [Python-Dev] PEP 455: TransformDict
- Next message: [Python-Dev] PEP 455: TransformDict
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Mon, Oct 07, 2013 at 06:17:09PM -0700, Mark Janssen wrote:
Sorry I missed the original discussion, but isn't this a simple case of putting a decorator around the getitem method (to transform the input key) and a single line in the body of the setitem method, making this very easy adaptation of the existing dict class?
Not really. We can try what you suggest to implement a case insensitive dict (decorator is not needed):
py> class CaseInsensitiveDict(dict): ... def getitem(self, key): ... key = key.casefold() # use .lower() before Python 3.3 ... return super().getitem(key) ... def setitem(self, key, value): ... key = key.casefold() ... super().setitem(key, value) ... py> d = CaseInsensitiveDict() py> d['X'] = 42 py> d {'x': 42}
Well, that's not exactly what I was hoping for... I was hoping that the dict would preserve case, rather than just convert it. But that's only the start of the problem:
py> d['X'] # this works 42 py> d.pop('X') # expecting 42 Traceback (most recent call last): File "", line 1, in KeyError: 'X'
So no, it isn't just a matter of a trivial wrapper around getitem, setitem and delitem.
Check the bug tracker for more detail:
http://bugs.python.org/issue18986
-- Steven
- Previous message: [Python-Dev] PEP 455: TransformDict
- Next message: [Python-Dev] PEP 455: TransformDict
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]