Which is a more efficient __hash__ implementation in Python objects? Hashing self.__str__, a tuple of attributes, or a real hash routine?
<p>It seems a common and quick way to create a stock <code>__hash__()</code> for any given Python object is to <code>return hash(str(self))</code>, if that object implements <code>__str__()</code>.  Is this efficient, though?  Per <a href="http://stackoverflow.com/a/4005412">this SO answer</a>, a hash of a tuple of the object's attributes is "good", but doesn't seem to indicate if it's the most efficient for Python.  Or would it be better to implement a <code>__hash__()</code> for each object and use a real hashing algorithm from <a href="http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx" rel="nofollow">this page</a> and mixup the values of the individual attributes into the final value returned by  <code>__hash__()</code>?</p>

<p>Pretend I've implemented the Jenkins hash routines from <a href="http://stackoverflow.com/a/4594659">this SO question</a>.  Which <code>__hash__()</code> would be better to use?:</p>

<pre><code># hash str(self)
def __hash__(self):
    return hash(str(self))

# hash of tuple of attributes
def __hash__(self):
    return hash((self.attr1, self.attr2, self.attr3,
                 self.attr4, self.attr5, self.attr6))

# jenkins hash
def __hash__(self):
    from jenkins import mix, final
    a = self.attr1
    b = self.attr2
    c = self.attr3
    a, b, c = mix(a, b, c)
    a += self.attr4
    b += self.attr5
    c += self.attr6
    a, b, c = final(a, b, c)
    return c
</code></pre>

<p><br />Assume the attrs in the sample object are all integers for simplicity.  Also assume that all objects derive from a base class and that each objects implements its own <code>__str__()</code>.  The tradeoff in using the first hash is that I could implement that in the base class as well and not add additional code to each of the derived objects.  But if the second or third <code>__hash__()</code> implementations are better in some way, does that offset the cost of the added code to each derived object (because each may have different attributes)?</p>

<p><br/><br/><strong>Edit:</strong> the <code>import</code> in the third <code>__hash__()</code> implementation is there only because I didn't want to draft out an entire example module + objects.  Assume that <code>import</code> really happens at the top of the module, not on each invocation of the function.</p>

<p><br/><br/><strong>Conclusion:</strong> Per the answer and comments on <a href="http://stackoverflow.com/q/10130454">this closed SO question</a>, it looks like I really want the tuple hash implementation, not for speed or efficiency, but because of the underlying duality of <code>__hash__</code> and <code>__eq__</code>.  Since a hash value is going to have a limited range of some form (be it 32 or 64 bits, for example), in the event you do have a hash collision, object equality is then checked.  So since I do implement <code>__eq__()</code> for each object by using tuple comparison of self/other's attributes, I also want to implement <code>__hash__()</code> using an attribute tuple so that I respect the hash/equality nature of things.</p>
