hash() algorithm?

Fredrik Lundh fredrik at effbot.org
Mon Dec 4 10:44:53 CET 2000

anand wrote:> Although I'm having trouble finding that file....
> Is the path diffent for the mac distribution?  Because I don't have a
> Objects directory, and stringobject.c doesn't exist anywhere.

no, it's not different -- stringobject.c is part of the inter-
preter's SOURCE code.  if you're looking at a binary release,
you won't find it.

source code is available here:


and here's the relevant portion of the string hash

static long
string_hash(PyStringObject *a)
 register int len;
 register unsigned char *p;
 register long x;

 len = a->ob_size;
 p = (unsigned char *) a->ob_sval;
 x = *p << 7;
 while (--len >= 0)
  x = (1000003*x) ^ *p++;
 x ^= a->ob_size;
 if (x == -1)
  x = -2;
 return x;

as I mentioned before, other data types use different
hash algorithms.  check the sources for details.


More information about the Python-list mailing list