How does the function hash()
compute the hash value of a tuple? For example:
t = (1,2,3)
print(hash(t))
Gives an output
-378539185
You can check the implementation of this function in C if you're familiar with C programming and some advanced math. It seems that algorithm XORs hashes of each element in tuple and adds some magic.
static Py_hash_t
tuplehash(PyTupleObject *v)
{
Py_uhash_t x; /* Unsigned for defined overflow behavior. */
Py_hash_t y;
Py_ssize_t len = Py_SIZE(v);
PyObject **p;
Py_uhash_t mult = _PyHASH_MULTIPLIER;
x = 0x345678UL;
p = v->ob_item;
while (--len >= 0) {
y = PyObject_Hash(*p++);
if (y == -1)
return -1;
x = (x ^ y) * mult;
/* the cast might truncate len; that doesn't change hash stability */
mult += (Py_hash_t)(82520UL + len + len);
}
x += 97531UL;
if (x == (Py_uhash_t)-1)
x = -2;
return x;
}
Note that this is CPython's current implementation. Other Python interpreters and even other versions of CPython may have different hash functions. This particular implementation, called SipHash, has been in use since 2013. See PEP 456 -- Secure and interchangeable hash algorithm for a detailed explanation.
SipHash is a cryptographic pseudo random function with a 128-bit seed and 64-bit output.... SipHash is a family of pseudorandom functions (a.k.a. keyed hash functions) optimized for speed on short messages. Target applications include network traffic authentication and defense against hash-flooding DoS attacks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With