But the sets and dicts cover the greatest part of the use of hash.
(Personally, I never used that explicitly in my programs)
Cyril
On 7/21/05, Michael Hudson <[EMAIL PROTECTED]> wrote:
Steven D'Aprano <[EMAIL PROTECTED]> writes:
> Do people often use hash() on built-in types?
Only implicitly.
> What do you find it useful for?
Dictionaries :)
> How about on custom classes?
Same here.
> Can anyone give me some good tips or hints for writing and using
> hash functions in Python?
Well, the usual tip for writing them is, don't, unless you need to.
If implement __eq__, then you need to, so it's fairly common to just
hash a tuple containing the things that are considered by the __eq__
method. Something like:
class C(object):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def __eq__(self, other):
return self.a == other.a and self.b == other.b
def __hash__(self):
return hash((self.a, self.b ))
Cheers,
mwh
--
I'm a keen cyclist and I stop at red lights. Those who don't need
hitting with a great big slapping machine.
-- Colin Davidson, cam.misc
--
http://mail.python.org/mailman/listinfo/python-list
-- http://mail.python.org/mailman/listinfo/python-list