[Python-ideas] An identity dict
jh at improva.dk
Tue Jun 1 09:52:44 CEST 2010
On 2010-06-01 03:23, Raymond Hettinger wrote:
> On May 31, 2010, at 5:45 PM, Benjamin Peterson wrote:
>> Essentially these are places where defined equality should not matter.
"should not matter" is the important part here. It might have been
clearer to say "should be ignored" instead. I think Raymond is
> Essentially, these are cases where an identity dictionary isn't
> necessary and would in-fact be worse performance-wise
> in every implementation except for PyPy which can compile
> the pure python code for indentity_dict.py.
It is necessary, because the objects involved might define their own
__hash__ and __cmp__/__eq__, and these should *not* be used.
> Sorry, but I think this idea is a total waste. Perhaps post it as
> a recipe, but it doesn't make sense to try to inject it into the
> standard library.
I don't think it is a total waste, but I have seen two ideas in this
thread that I find more generally useful. One is
"collections.keyfuncdict", which could be trivially used as an
identitydict. The other is a WeakIdentityDict, which is a WeakKeyDict
that uses only the identity of the keys for hashing/equality. These two
are independent, one cannot be used to implement the other (unless
collections.keyfuncdict grows an option to not keep strong refs to the
keys, perhaps by providing the inverse keyfunc instead).
Anyway, +0.1 on identitydict and +1 on each of collection.keyfuncdict
More information about the Python-ideas