python hash() function
Dan Bishop
danb_83 at yahoo.com
Tue Mar 25 22:30:05 EDT 2008
On Mar 25, 9:22 pm, "Terry Reedy" <tjre... at udel.edu> wrote:
> "Alvin Delagon" <adela... at gmail.com> wrote in message
>
> news:7a01f6c00803251905j4b12633m65b2cde0f3037854 at mail.gmail.com...
> | Hello,
> |
> | >>> hash("foobar")
> | -1969371895
> |
> | Anyone can explain to me how the hash() function in python does its work?
> A
> | link to its source could help me a lot also. I'm looking for a way to
> | replicate this function in php. Thanks in advance.
>
> If not part of your installation, start with svn.python.org and click
> browse. I would look for builtins.c or somesuch.
It's in stringobject.c:
static long
string_hash(PyStringObject *a)
{
register Py_ssize_t len;
register unsigned char *p;
register long x;
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;
}
More information about the Python-list
mailing list