keying by identity in dict and set

Chris Angelico rosuav at gmail.com
Sun Oct 27 04:02:14 EDT 2019


On Sun, Oct 27, 2019 at 6:26 PM Steve White <stevan.white at gmail.com> wrote:
> As near as I can tell, returning the id() in __hash__() results in a
> perfect hash key.  I really want to know if that is true.
> Because if it is true, any further layer is simply covering for a
> failing in the documentation.

Only if your __eq__() does not return True for anything other than
itself. That is the entire definition of __hash__ - it needs to return
the same value for two objects that are equal. This is exactly what
the documentation said, as multiple people have posted here.

If your object is equal only to itself, then its hash can be its
identity. Otherwise it should not. It is that simple.

ChrisA



More information about the Python-list mailing list