Fwd: keying by identity in dict and set

dieter dieter at handshake.de
Wed Oct 30 02:05:40 EDT 2019


Steve White <stevan.white at gmail.com> writes:
> ...
> A little documentation would have saved me personally days of work.
> It  would be helpful to know:
>   * under what conditions can one expect a "perfect hash", that is,
> one where __eq__() will never be called?

Never expect it: keys with different hash values may end
up in the same "dict" slot - and then "__eq__" may
be used for disambiguation even with a perfect hash function.

>   * is it sufficient to return the value of the key object's id()
> function to produce a perfect hash?

Usually.

Exceptions are (maybe among others): persistent objects (i.e.
objects which live longer than a process), situations during
shutdown (during shutdown, an object may be released
even if there are still references to it; it might
be possible that during this phase the same address is reused
even if at some other place there is still an implicit use
of the old object).

>   * when might it be useful to consider keying by identity?

Suboptimal question.

Follow Python's standard "dict" key management paradigm (which
is equality based). If this does not fit, then look
for the approach that fits your **special** case.

>   * what are the limitations of programming this way?

With "this way", you mean "identity based dicts"?
Then, you have already got several limitations in this thread.



More information about the Python-list mailing list