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
?
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']