rhombidodecahedron
rhombidodecahedron

Reputation: 7922

What is the best integer n-tuple hash in python?

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

Answers (1)

mgilson
mgilson

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

Related Questions