Python's Curious Case of hash(-1) == hash(-2)

2025-01-10

A Reddit question about why `hash(-1) == hash(-2)` in Python sparked an investigation. By diving into the CPython source code, the author discovered that -1 is used as an error indicator in the `PyObject_Hash` function. Therefore, when the calculated hash is -1, it's converted to -2. This isn't a Python Easter egg, but rather a quirk of how CPython's C code handles errors.