擅长:python、mysql、java
<blockquote>
<p>Use the built-in hash() function. This function, at least on the machine I'm developing for (with
python 2.7, and a 64-bit cpu) produces an integer that fits within 32 bits - not large enough for
my purposes.</p>
</blockquote>
<p>那不是真的。内置哈希函数将在64位系统上生成64位哈希。</p>
<p>这是来自<code>Objects/stringobject.c</code>(python版本2.7)的python str哈希函数:</p>
<pre><code>static long
string_hash(PyStringObject *a)
{
register Py_ssize_t len;
register unsigned char *p;
register long x; /* Notice the 64-bit hash, at least on a 64-bit system */
if (a->ob_shash != -1)
return a->ob_shash;
len = Py_SIZE(a);
p = (unsigned char *) a->ob_sval;
x = *p << 7;
while (--len >= 0)
x = (1000003*x) ^ *p++;
x ^= Py_SIZE(a);
if (x == -1)
x = -2;
a->ob_shash = x;
return x;
}
</code></pre>