mwh at python.net
Thu Jul 21 18:42:22 CEST 2005
Steven D'Aprano <steve at REMOVETHIScyber.com.au> writes:
> Do people often use hash() on built-in types?
> What do you find it useful for?
> How about on custom classes?
> 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:
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
return hash((self.a, self.b))
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
More information about the Python-list