Tuple hashing in Python causing different results on different systems

I trained tuple hashing. There I worked on Python 2.7. Below is the code:

num = int(raw_input()) num_list = [int(x) for x in raw_input().split()] print(hash(tuple(num_list))) 

The above code leads to

 >>> 2 >>> 1 2 >>> 3713081631934410656 

But on my local PC, where I use Python 3.4, the answer is

 >>> 1299869600 

The code is accepted, but I could not find out what causes the different results. Is this for another version of Python?

+5
source share
1 answer

hash() can return different values ​​for the same object in different operating systems, architectures, Python implementations, and Python versions.

It is intended to be used only within a single Python session, and not through sessions or machines. Therefore, you should never rely on the hash() value outside of this.

If you need hashing that produces the same results worldwide, consider checksums such as:

  • MD5 or SHA1,
  • xxHash, which on its author provides stable results for several OS and architecture, be it small or large endian, 32/64 bit, posix or not, etc.).
  • or with some caution Murmur, as some versions may give different results on different architectures. For example, I tested this fist hand when porting C Murmur2 to an IBM S390 Linux installation (from all of the weird places!). To avoid problems, I ended up coding instead of a slow but arc independent clean Python implementation for this OS, not a C implementation.
+1
source

Source: https://habr.com/ru/post/1237303/


All Articles