[Python-ideas] An identity dict
Raymond Hettinger
raymond.hettinger at gmail.com
Mon May 31 19:25:30 CEST 2010
On May 30, 2010, at 5:30 PM, Greg Ewing wrote:
> Benjamin Peterson wrote:
>> Lie Ryan <lie.1296 at ...> writes:
>>> what's wrong with dict[id(key)] = foo?
>> For one, you can't get the value of the key out of the dict.
>
> Another thing is that it doesn't keep the key object
> alive, so if the app doesn't do anything else to ensure
> this, it's possible for the key to disappear and be
> replaced by another object having the same id.
If you don't have an external reference to the object,
how are you ever going to look it up in the dictionary?
Since we can attach values directly to many objects
(using instance variables, function attributes, etc)
or can keep them in a tuple, there appear to be remarkably
few use cases for an identity dictionary.
Also, there hasn't been much discussion of implementation,
but unless you're willing to copy and paste most of the
code in dictobject.c, you're going to end-up with something
much slower than d[id(obj)]=value.
Raymond
More information about the Python-ideas
mailing list