Skip to content

"hashable collections" in the object.__hash__ documentation is misleading #158798

Description

@dluschan

Documentation

Documentation: https://docs.python.org/3/reference/datamodel.html#object.__hash__

The object.__hash__ documentation uses the phrase "hashable collections", and in one place links it to the glossary term "hashable". This is inaccurate, and it is inconsistent with the first paragraph of the same entry.

Current text, second paragraph:

If a class does not define an __eq__() method it should not define a __hash__() operation either; if it defines __eq__() but not __hash__(), its instances will not be usable as items in hashable collections. If a class defines mutable objects and implements an __eq__() method, it should not implement __hash__(), since the implementation of [hashable] collections requires that a key's hash value is immutable (if the object's hash value changes, it will be in the wrong hash bucket).

([hashable] is a link to the glossary term.)

Problem

The glossary defines "hashable" as a property of an object: it has a hash value that never changes during its lifetime and can be compared to other objects. The glossary also says that mutable containers such as lists and dictionaries are not hashable.

"Hashable collections" therefore reads as "collections that can be hashed". That is not what is meant, and set and dict are not hashable in that sense. What is meant is collections that use hashing internally and need their keys or elements to be hashable. Linking the phrase to the glossary entry makes the wrong reading more likely.

The first paragraph of the same entry says "hashed collections including set, frozenset, dict, and frozendict", so the terminology is also inconsistent within one entry.

Suggested change

Use "hash-based collections" consistently and drop the glossary link from that phrase:

...its instances will not be usable as items in hash-based collections. If a class defines mutable objects and implements an __eq__() method, it should not implement __hash__(), since the implementation of hash-based collections requires that a key's hash value is immutable...

The first paragraph could then say "hash-based collections" instead of "hashed collections", so one term is used throughout.

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions