Timeline for answer to What is the default __hash__ in python? by asmeurer
Current License: CC BY-SA 3.0
Post Revisions
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 26, 2023 at 13:47 | comment | added | Karl Knechtel |
As of 3.12 it still works this way, although it's been refactored. This is the default implementation found in the base object type; any other type (such as str) is free to override it.
|
|
| Jan 4, 2019 at 17:18 | comment | added | David Munro | size_t is unsigned in C (not signed), so as the comment remarks, this is a roll operation | |
| May 23, 2017 at 12:25 | history | edited | URL Rewriter Bot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
| Mar 15, 2016 at 16:55 | comment | added | Piotr Dobrogost | I think Duncan meant hash randomization introduced in Python 3.3. However it's currently only active for strings and the code you show is probably for general case. | |
| Mar 15, 2016 at 16:25 | history | edited | asmeurer | CC BY-SA 3.0 |
show that the hash does indeed come from the id()
|
| Mar 15, 2016 at 16:20 | comment | added | asmeurer |
@PiotrDobrogost there is a fixed relationship. It's (id(x) >> 4) | (id(x) << (8 * SIZEOF_VOID_P - 4)). The code I pasted here is taken from the Python 3 source. d (the input to the _Py_HashPointer function) is the memory address of the object, i.e., its id(). Run SIZEOF_VOID_P = 8; y = numpy.array([4325845928], dtype='int64'); print((y >> 4) | (y << (8 * SIZEOF_VOID_P - 4))). The result is -9223372036584410438, which corresponds to the example I showed above.
|
|
| Mar 15, 2016 at 12:36 | comment | added | Piotr Dobrogost |
According to Duncan – In Python 3.3 there won't even be a fixed relationship between id() and hash().
|
|
| Nov 6, 2015 at 22:06 | history | edited | asmeurer | CC BY-SA 3.0 |
remove stuff that might be wrong
|
| Nov 6, 2015 at 17:31 | history | answered | asmeurer | CC BY-SA 3.0 |