[Python-Dev] Add a "transformdict" to collections

Richard Oudkerk shibturn at gmail.com
Tue Sep 10 16:40:55 CEST 2013


On 10/09/2013 3:15pm, Armin Rigo wrote:
> Hi Richard,
>
> On Tue, Sep 10, 2013 at 3:42 PM, Richard Oudkerk <shibturn at gmail.com> wrote:
>> I guess another example is creating an "identity dict" (see
>> http://code.activestate.com/lists/python-ideas/7161/) 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
True
 >>> [1,2,3] in d
False
 >>> print(d[L])
None
 >>> 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\abc.py", line 444, in 
__repr__
     return '{0.__class__.__name__}({0._mapping!r})'.format(self)
   File "C:\Repos\cpython-dirty\lib\collections\__init__.py", line 944, 
in __repr__
     self._transform, repr(dict(self)))
TypeError: unhashable type: 'list'

--
Richard


More information about the Python-Dev mailing list