File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -583,11 +583,16 @@ are always available. They are listed here in alphabetical order.
583583
584584.. function :: hash(object)
585585
586- Return the hash value of the object (if it has one). Hash values are integers.
587- They are used to quickly compare dictionary keys during a dictionary lookup.
588- Numeric values that compare equal have the same hash value (even if they are of
589- different types, as is the case for 1 and 1.0).
586+ Return the hash value of the object (if it has one). Hash values are
587+ integers. They are used to quickly compare dictionary keys during a
588+ dictionary lookup. Numeric values that compare equal have the same hash
589+ value (even if they are of different types, as is the case for 1 and 1.0).
590590
591+ .. note ::
592+
593+ For object's with custom :meth: `__hash__ ` methods, note that :func: `hash `
594+ truncates the return value based on the bit width of the host machine.
595+ See :meth: `__hash__ ` for details.
591596
592597.. function :: help([object])
593598
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -210,6 +210,9 @@ Tests
210210Documentation
211211-------------
212212
213+ - Issue #18440: Clarify that `hash()` can truncate the value returned from an
214+ object's custom `__hash__()` method.
215+
213216- Issue #17953: Mention that you shouldn't replace sys.modules and deleting key
214217 items will cause Python to not be happy.
215218
You can’t perform that action at this time.
0 commit comments