[Python-ideas] An identity dict

Raymond Hettinger raymond.hettinger at gmail.com
Tue Jun 1 06:37:05 CEST 2010

On May 31, 2010, at 7:31 PM, Benjamin Peterson wrote:

> Lie Ryan <lie.1296 at ...> writes:
>> that their id() is expensive is implementation details, and the
>> developer of PyPy should solve that instead of adding a clutch to the
>> stdlib.
> The stdlib isn't just about CPython. We already have optimized primitives for
> CPython, so I don't see why helping other implementations isn't a good cause.

Benjamin, could you elaborate of several points that are unclear:

* If id() is expensive in PyPy, then how are they helped by the code in 
which uses id() for the gets and sets and contains?

* In the examples you posted (such as http://codespeak.net/svn/pypy/trunk/pypy/tool/algo/graphlib.py ),
it appears that PyPy already has an identity dict,  so how are they helped by adding one to the collections module?

* Most of the posted examples already work with regular dicts (which check identity before they check equality) -- don't the other implementations already implement regular dicts which need to have identity-implied-equality in order to pass the test suite?  I would expect the following snippet to work under all versions and implementations of Python:

    >>> class A: 
    ...         pass
    >>> a = A()
    >>> d = {a: 10}
    >>> assert d[a] == 10   # uses a's identity for lookup

* Is the proposal something needed for all implementations or is it just an optimization for a particular, non-CPython implementation?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100531/a311aef1/attachment.html>

More information about the Python-ideas mailing list