Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4958f71

Browse files
committed
- Issue #18440: Clarify that hash() can truncate the value returned from an
object's custom `__hash__()` method.
2 parents 0fedb37 + 224a599 commit 4958f71

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

Doc/library/functions.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,16 @@ are always available. They are listed here in alphabetical order.
587587

588588
.. function:: hash(object)
589589

590-
Return the hash value of the object (if it has one). Hash values are integers.
591-
They are used to quickly compare dictionary keys during a dictionary lookup.
592-
Numeric values that compare equal have the same hash value (even if they are of
593-
different types, as is the case for 1 and 1.0).
590+
Return the hash value of the object (if it has one). Hash values are
591+
integers. They are used to quickly compare dictionary keys during a
592+
dictionary lookup. Numeric values that compare equal have the same hash
593+
value (even if they are of different types, as is the case for 1 and 1.0).
594594

595+
.. note::
596+
597+
For object's with custom :meth:`__hash__` methods, note that :func:`hash`
598+
truncates the return value based on the bit width of the host machine.
599+
See :meth:`__hash__` for details.
595600

596601
.. function:: help([object])
597602

Doc/reference/datamodel.rst

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,10 +1264,21 @@ Basic customization
12641264

12651265
Called by built-in function :func:`hash` and for operations on members of
12661266
hashed collections including :class:`set`, :class:`frozenset`, and
1267-
:class:`dict`. :meth:`__hash__` should return an integer. The only required
1268-
property is that objects which compare equal have the same hash value; it is
1269-
advised to somehow mix together (e.g. using exclusive or) the hash values for
1270-
the components of the object that also play a part in comparison of objects.
1267+
:class:`dict`. :meth:`__hash__` should return an integer. The only
1268+
required property is that objects which compare equal have the same hash
1269+
value; it is advised to somehow mix together (e.g. using exclusive or) the
1270+
hash values for the components of the object that also play a part in
1271+
comparison of objects.
1272+
1273+
.. note::
1274+
1275+
:func:`hash` truncates the value returned from an object's custom
1276+
:meth:`__hash__` method to the size of a :c:type:`Py_ssize_t`. This is
1277+
typically 8 bytes on 64-bit builds and 4 bytes on 32-bit builds. If an
1278+
object's :meth:`__hash__` must interoperate on builds of different bit
1279+
sizes, be sure to check the width on all supported builds. An easy way
1280+
to do this is with
1281+
``python -c "import sys; print(sys.hash_info.width)"``
12711282

12721283
If a class does not define an :meth:`__eq__` method it should not define a
12731284
:meth:`__hash__` operation either; if it defines :meth:`__eq__` but not

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,9 @@ Tests
581581
Documentation
582582
-------------
583583

584+
- Issue #18440: Clarify that `hash()` can truncate the value returned from an
585+
object's custom `__hash__()` method.
586+
584587
- Issue #17844: Add links to encoders and decoders for bytes-to-bytes codecs.
585588

586589
- Issue #14097: improve the "introduction" page of the tutorial.

0 commit comments

Comments
 (0)