Hashing tuple in Python causing different results in different systems

2024/11/16 15:37:25

I was practicing tuple hashing. In there I was working 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 results in

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

But at my local PC where I am using 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 different version of Python?

Answer

The hash() may return different values for the same object on different OS, architectures, Python implementations and Python versions.

It is designed to be used only within a single Python session, not across sessions or machines. So you should never rely on the value of hash() beyond this.

If you need hashing that yields the same results everywhere, consider checksums such as:

  • MD5 or SHA1,
  • xxHash which per its author provides stable results across multiple OS and architecture, be it little or big endian, 32/64 bits, posix or not, etc.)
  • or with some caution Murmur as some versions may yield different results on different architectures. For instance I experienced this fist hand when porting a C Murmur2 to an IBM S390 Linux install (of all odd places!). To avoid issues I ended instead coding a slow but arch-independent pure Python implementation on that OS rather than a C implementation.
https://en.xdnf.cn/q/71330.html

Related Q&A

ctypes pointer into the middle of a numpy array

I know how to get a ctypes pointer to the beginning of a numpy array:a = np.arange(10000, dtype=np.double) p = a.ctypes.data_as(POINTER(c_double)) p.contents c_double(0.0)however, I need to pass the po…

Extracting unsigned char from array of numpy.uint8

I have code to extract a numeric value from a python sequence, and it works well in most cases, but not for a numpy array.When I try to extract an unsigned char, I do the followingunsigned char val = b…

How to hold keys down with pynput?

Im using pynput and I would like to be able to hold keys down, specifically wasd but when I try and run this code it only presses the key and doesnt hold it for 2 seconds. If anyone knows what Im doing…

How well does your language support unicode in practice?

Im looking into new languages, kind of craving for one where I no longer need to worry about charset problems amongst inordinate amounts of other niggles I have with PHP for a new project.I tend to fin…

analogy to scipy.interpolate.griddata?

I want to interpolate a given 3D point cloud:I had a look at scipy.interpolate.griddata and the result is exactly what I need, but as I understand, I need to input "griddata" which means some…

Mongodb TTL expires documents early

I am trying insert a document into a Mongo database and have it automatically expire itself after a predetermine time. So far, my document get inserted but always get deleted from the database from 0 -…

sqlalchemy, hybrid property case statement

This is the query Im trying to produce through sqlalchemySELECT "order".id AS "id", "order".created_at AS "created_at", "order".updated_at AS "u…

Whats the newest way to develop gnome panel applets (using python)

Today Ive switched to GNOME (from XFCE) and found some of the cool stuff missing and I would like to (try to) do them on my own. I tried to find information on how to develop Gnome applets (items you p…

How to install opencv-python in python 3.8

Im having problem during the installation of opencv-python in pycharm. After opening pycharm I click on settings and then project interpreter, I click on + and search for the correct module, I started …

python augmented assignment for boolean operators

Does Python have augmented assignment statements corresponding to its boolean operators?For example I can write this:x = x + 1or this:x += 1Is there something I can write in place of this:x = x and yT…