You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The reference implementation of Python is "CPython", which is almost certainly the Python you're using. CPython is written in C, and unlike Python, C doesn't have exceptions. So, in C, when you design a function, and you want your function to be able to indicate "there was an error", it has to return that error as its return value.
The hash() function in CPython can return an error, so it defines a return code of -1 to mean "there was an error". But this could be confusing if the hash worked correctly, and the actual hash of an object was -1. So the convention is: if hashing worked, and you got a -1, return -2.
There's special code in the hash function for ints ("long objects") in CPython that does exactly that:
Description
Small integers hash to themselves, except -1.
Snippet preview
Explanation
See https://omairmajid.com/posts/2021-07-16-why-is-hash-in-python/ and https://www.reddit.com/r/Python/comments/oks5km/comment/h5a7ylc/
It's explained by
u/ExoticMandibles
on Reddit:And the mentioned CPython code snippet is:
Checklist before calling for maintainers
Explanation
section? It shall include the reasons for changes and why you'd like us to include themThe text was updated successfully, but these errors were encountered: