[Python-Dev] gc ideas -- sparse memory

"Martin v. Löwis" martin at v.loewis.de
Sat Dec 4 20:37:14 CET 2010

> Why is useful to expose an identity hash?  AFAICS it is *only* useful
> in building an identity hash table.  If so, why not just provide id()
> or the is operator or both and be done with it?

That's precisely James' point: Java provides the identity hash
*instead* of the id() function (i.e. it does not have an equivalent
of id()). Doing so gives greater liberties in implementing Java.
For example, an implementation that would only use the type and
not the instance for identity hash would still be conforming
(as would one that always returns 0).

> With identity, I don't see much point in leaving that up to the
> programmer.  In implementations with stable object addresses, the
> address is a fine implementation of identity_hash() -- perfect, in
> fact.

Of course. James' complaint is that Python-the-language mandates
support for an id() function, though - a requirement that even
implementations that don't have stable object addresses now must
support. If Python mandated only identity hash properties of the
id() function, alternative implementations could be simplified.

> ISTM that's no problem, you have to do an extra comparison for
> identity when the codes are equal either way.  Of course the identity
> hash code is a more precise optimization, but that doesn't make hash()
> unusable for this purpose (cf Sun's built-in implementation of
> IdentityHashCode that always returns 2).

That's only the case if the hash() result is guaranteed not
to change. In some applications, it may be desirable to have that
as an absolute guarantee (rather than just being a convention).

No, you can't substitute identity hash with hash: the value hash of an
object may change over time, whereas the identity hash must not.


More information about the Python-Dev mailing list