hashkey/digest for a complex object

Hrvoje Niksic hniksic at xemacs.org
Sun Oct 10 12:24:21 EDT 2010


Arnaud Delobelle <arnodel at gmail.com> writes:

> I have learnt too that hash(-1) is not (-1), and that it seems that a
> hash value is not allowed to be (-1).  There is one thing left to find
> out.  Why can't it be (-1)?

Because -1 has a special meaning in the C function equivalent to
Python's hash().  PyObject_Hash returning -1 means that an exception was
raised somewhere inside the object's __hash__ method.  For that reason
hash functions that really return -1 must change that to another value,
and -2 is as good a replacement as any.

This is documented in
http://docs.python.org/c-api/object.html?highlight=pyobject_hash#PyObject_Hash



More information about the Python-list mailing list