[Python-Dev] gc ideas -- sparse memory
"Martin v. Löwis"
martin at v.loewis.de
Sat Dec 4 13:22:07 CET 2010
> Surely even in Java or C#, objects have an *identity* even if the
> language doesn't provide a way to query their distinct *identification*.
When people said "the id of an object should this or that", they always
meant identification, not identity (perhaps except for you). Nobody
would propose that the identity (in your wording) of two distinct
objects might be the same, or might change over time.
> I'm afraid James' explanation didn't shed any light on the question to
> me. It seems to me that Java's IdentityHashValue[sic -- I think the
> correct function name is actually IdentityHashCode] is equivalent to
> Python's hash()
No, it's not. java.lang.Object.hashCode() is equivalent to Python's
hash(). In particular, for both of these, the following requirement
holds: object that *compare* equal must hash equal.
This is not the case for the identity hash code, or Python's id
function: objects that compare equal may still have different ids,
or different java identity hash codes.
> not to Python's id(), and claiming it is related to
> identity is misleading and confusing.
No, it's right on spot. This is *not* about the regular value hash,
but about the identity hash.
> Like hash(), IdentityHashCode doesn't make any promises about identity
> at all.
It sure does: calling identityHashCode on the very same object twice
will always return the same value - even after a garbage collection.
So if two references refer to the same object, calling identityHashCode
on both of them gives the same value.
This is similar to the relationship between equals and hashCode.
> Two distinct objects could have the same hash code, and a
> further test is needed to distinguish them.
Correct. However, references with different identity hash codes will
never refer to the same object.
>>> Why would you bother using that function when you could just use
>>> x == y instead?
>> Because in a hash table, you also need a hash value.
> Well, sure, in a hash table you need a hash value. But I was talking
> about an id() function.
> So is that it? Is IdentityHashValue (or *Code, as the case may be) just
> a longer name for hash()?
Of course not.
More information about the Python-Dev