[Tutor] object representation
steve at pearwood.info
Fri Mar 5 02:21:46 CET 2010
On Fri, 5 Mar 2010 01:22:52 am Dave Angel wrote:
> spir wrote:
> > PS: Would someone point me to typical hash funcs for string keys,
> > and the one used in python?
But note that this was written a few years ago, and so may have been
As for "typical" hash functions, I don't think there is such a thing. I
think everyone creates there own, unless there is a built-in hash
function provided by the language or the library, which is what Python
does. I've seen hash functions as simple as:
if not s: return 0
else: return ord(s)
but of course that leads to *many* collisions.
Slightly better (but not much!) is
n = 0
for c in s:
n += ord(c)
return n % sys.maxint
This is a pretty awful hash function though. Don't use it.
You might also like to read this thread, to get an idea of the thought
that has been put into Python's hashing:
> I figure every object has exactly three items in it: a ref count, a
> implementation pointer, and a payload.
Not quite. CPython has ref counts. IronPython and Jython don't. Other
Pythons may or may not.
I'm not quite sure what you mean by "implementation pointer" -- I think
you're talking about a pointer back to the type itself. It's normal to
just to refer to this as "the type", and ignore the fact that it's
actually a pointer. The type data structure (which itself will be an
object) itself is not embedded in every object! And of course, other
Pythons may use some other mechanism for linking objects back to their
More information about the Tutor