Python: Are `hash` values for built-in numeric types, strings standardised?

2024/11/23 18:23:47

I came to this question while pondering about the ordering of set, frozenset and dict. Python doesn't guarantee any ordering, and any ordering is coupled to the hash value at some level. But is the hash value for a value of a numeric or string built-in type standardized? In other words, would

hash((a,b,c,d,e,f,g))

have a determined value, if a, b, c, d, e, f, g are numeric values or str?

Answer

The hash values for strings and integers are absolutely not standardized. They could change with any new implementation of Python, including between 2.6.1 and 2.6.2, or between a Mac and a PC implementation of the same version, etc.

More importantly, though, stable hash values doesn't imply repeatable iteration order. You cannot depend on the ordering of values in a set, ever. Even within one process, two sets can be equal and not return their values in the same order. This can happen if one set has had many additions and deletions, but the other has not:

>>> a = set()
>>> for i in range(1000000): a.add(str(i))
...
>>> for i in range(6, 1000000): a.remove(str(i))
...
>>> b = set()
>>> for i in range(6): b.add(str(i))
...
>>> a == b
True
>>> list(a)
['1', '5', '2', '0', '3', '4']
>>> list(b)
['1', '0', '3', '2', '5', '4']
https://en.xdnf.cn/q/120716.html

Related Q&A

How to create a sample django project?

This doesnt work for me.$ python django-admin.py startproject myprojectI am running a ubuntu virtual m/c on my windows system. By default ubuntu 12.04 comes with python 2.7.3 so I am using that only I …

Why elif statement instead of if statement? [duplicate]

This question already has answers here:Difference between multiple ifs and elifs?(9 answers)Why we use if, else if instead of multiple if block if the body is a return statement(13 answers)Closed 7 ye…

How to understand regular expression with python?

Im new with python. Could anybody help me on how I can create a regular expression given a list of strings like this:test_string = "pero pero CC tan tan RGantigua antiguo AQ0FS0que que CS segn se…

How do I reverse words in a string with Python [duplicate]

This question already has answers here:Reversing words in a Python string (including punctuation)(5 answers)Closed last month.I am trying to reverse words of a string, but having difficulty, any assist…

Reading input files and writing into output files - Python

I have an input file (input.txt) with the following information:Number of students (first line) Number of test scores (second line) list of student names and scoresSo the text file looks something like…

Get strings list in python with regex [duplicate]

This question already has answers here:Split string with multiple-character delimiter(3 answers)Closed 6 years ago.I want extract strings from this text with regex:~ZCC0ZAF~World~AAEef~RZgthAD~AAjaKNed…

python static code analysis tools - code analysis (preliminary research question) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

how can I get all post of users in django [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python 3: Getting IndexError: list index out of range on shuffle method

Im building a blackjack command line game and Ive run into a snag. The shuffle feature on my deck class object keeps coming up with IndexError: list index out of range on line 29. It is a sporadic bug…

How to encrypt a file

Just trimmed this down big timeI have an overall assignment that must read a file, encrypt it and then write the encrypted data to a new file.what ive tried is this:filename=input("Enter file name…