[Python-Dev] Why should the default hash(x) == id(x)?

"Martin v. Löwis" martin at v.loewis.de
Sat Nov 5 22:41:21 CET 2005

Noam Raphael wrote:
> Is there a reason why the default __hash__ method returns the id of the objects?

You are asking "why" question of the kind which are best answered as 
"why not".

IOW, you are saying that the current behaviour is bad, but you are not
proposing any alternative behaviour. There are many alternatives
possible, and they are presumably all worse than the current

To give an example: "why does hash() return id()"?
Answer: The alternative would be that hash() returns always 0 unless
implemented otherwise. This would cause serious performance issues
for people using the objects as dictionary keys. If they don't do that,
it doesn't matter what hash() returns.

> This leads me to another question: why should the default __eq__
> method be the same as "is"?

Because the alternative would be to always return "False". This
would be confusing, because it would cause "x == x" to give False.

More generally, I claim that the current behaviour is better than
*any* alternative. To refute this claim, you would have to come
up with an alternative first.


More information about the Python-Dev mailing list