Skip to content

Commit 665b7df

Browse files
authored
Improve hash() builtin docstring with caveats. (GH-125229)
Improve `hash()` builtin docstring with caveats. Mention its return type and that the value can be expected to change between processes (hash randomization). Why? The `hash` builtin gets reached for and used by a lot of people whether it is the right tool or not. IDEs surface docstrings and people use pydoc and `help(hash)`.
1 parent 618b726 commit 665b7df

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

Python/bltinmodule.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,15 +1840,17 @@ hash as builtin_hash
18401840
obj: object
18411841
/
18421842
1843-
Return the hash value for the given object.
1843+
Return the integer hash value for the given object.
18441844
1845-
Two objects that compare equal must also have the same hash value, but the
1846-
reverse is not necessarily true.
1845+
Two objects that compare equal must also have the same hash value, but
1846+
the reverse is not necessarily true. Hash values may differ between
1847+
Python processes. Not all objects are hashable; calling hash() on an
1848+
unhashable object raises TypeError.
18471849
[clinic start generated code]*/
18481850

18491851
static PyObject *
18501852
builtin_hash(PyObject *module, PyObject *obj)
1851-
/*[clinic end generated code: output=237668e9d7688db7 input=58c48be822bf9c54]*/
1853+
/*[clinic end generated code: output=237668e9d7688db7 input=70a242ff65f6717c]*/
18521854
{
18531855
Py_hash_t x;
18541856

Python/clinic/bltinmodule.c.h

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)