Reputation: 7922
Adopting from the Java Array class (lines 2938-2947) we can do:
def java_like_hash(tup):
return reduce(lambda x,y: 31*x+y, (1,)+tup)
But is this optimal for python?
Upvotes: 1
Views: 1515
Reputation: 309929
In python, if all the elements of a tuple can be hashed, so can the tuple. As such,
hash(tup)
should work just fine (and it'll be implemented in a way that is considered optimal by the python implementation developers).
Upvotes: 4