Could a more generic variant of this class work? In the same way that `sorted` can accept a comparison function, similar could be done for a dictionary-like class:

d = transformdict(key=str.lower)

Strictly speaking, this would provide case-insensitive but not case-preserving behaviour. For any given use case, though, a function could instead be supplied to "normalise" the key (upper, lower, title case, etc) in a way that fits that case. I can't think of many real cases where multiple types of capitalisation would be useful within the same dictionary.


On 10 September 2013 15:40, Richard Oudkerk <> wrote:
On 10/09/2013 3:15pm, Armin Rigo wrote:
Hi Richard,

On Tue, Sep 10, 2013 at 3:42 PM, Richard Oudkerk <> wrote:
I guess another example is creating an "identity dict" (see by doing

     d = transformdict(id)

This is bogus, because only the id will be stored, and the original
key object will be forgotten (and its id probably reused).

Seems to work for me:

>>> import collections
>>> d = collections.transformdict(id)
>>> L = [1,2,3]
>>> d[L] = None
>>> L in d
>>> [1,2,3] in d
>>> print(d[L])
>>> d._data
{41444136: ([1, 2, 3], None)}
>>> list(d)
[[1, 2, 3]]

However __repr__() is broken:

>>> d
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Repos\cpython-dirty\lib\collections\", line 444, in __repr__
    return '{0.__class__.__name__}({0._mapping!r})'.format(self)
  File "C:\Repos\cpython-dirty\lib\collections\", line 944, in __repr__
    self._transform, repr(dict(self)))
TypeError: unhashable type: 'list'