title: Alternative to python hash function for arbitrary objects
body: <p>In python2.7, I'm successfully using <code>hash()</code> to place objects into buckets stored persistently on disk. A mockup code looks like this:</p>
  
  <pre><code>class PersistentDict(object):
    def __setitem__(self, key, value):
      bucket_index = (hash(key)&amp;0xffffffff) % self.bucket_count
      self._store_to_bucket(bucket_index, key, value)
  
    def __getitem__(self, key):
      bucket_index = (hash(key)&amp;0xffffffff) % self.bucket_count
      return self._fetch_from_bucket(bucket_index)[key]
  </code></pre>
  
  <p>In python3, <code>hash()</code> uses a random or fixed salt, which makes it unusable/suboptimal for this [1]. Apparently, it's not possible to use a <a href="https://stackoverflow.com/q/38001874/5349916">fixed salt for specific invocations</a>. So, I need an alternative:</p>
  
  <ul>
  <li>Must be stable across interpreter invocations</li>
  <li>May require parameters supplied at execution time, e.g. setting a salt in the call</li>
  <li>Must support arbitrary objects (anything supported by <code>dict</code>/<code>set</code>)</li>
  </ul>
  
  <p>I've already tried using hash functions from <a href="https://docs.python.org/3/library/hashlib.html" rel="nofollow noreferrer">hashlib</a> (slow!) and checksums from <a href="https://docs.python.org/3/library/zlib.html#zlib.adler32" rel="nofollow noreferrer">zlib</a> (apparently not ideal for hashing, but meh) which work fine with strings/bytes. However, they work <em>only</em> on bytes-like objects, whereas <code>hash()</code> works with almost everything.</p>
  
  <hr>
  
  <p>[1] Using <code>hash()</code> to identify buckets is either:</p>
  
  <ul>
  <li>Not reliable across interpreter invocations, if salts are random</li>
  <li>Prevents applications from using the random salting feature, if salts are fixed</li>
  <li>Unusable if two <code>PersistentDict</code>s were created with different salts</li>
  </ul>