Danny Yoo wrote:

> Moreover, most implementations *deliberately* randomize their iteration
> order to avoid a particular kind of hash collision attack out there in
> the wild.  See:
 
In CPython the hash() of a string may change between different runs to fend 
off hash collision attacks, but that does not necessarily change the order 
of iteration:

In Python 3.4 for example both order and hash value change

$ python3.4 -c 'd = dict.fromkeys("foo bar baz".split()); print(hash("foo"), 
*d)'
-1599197652882818545 baz bar foo
$ python3.4 -c 'd = dict.fromkeys("foo bar baz".split()); print(hash("foo"), 
*d)'
-7773300350121034240 foo bar baz
$ python3.4 -c 'd = dict.fromkeys("foo bar baz".split()); print(hash("foo"), 
*d)'
4096077922251392823 baz bar foo

In Python 3.6 on the other side the hash values still change, but 
(insertion) order is preserved:

$ python3.6 -c 'd = dict.fromkeys("foo bar baz".split()); print(hash("foo"), 
*d)'
22453670082131454 foo bar baz
$ python3.6 -c 'd = dict.fromkeys("foo bar baz".split()); print(hash("foo"), 
*d)'
4601604916521659267 foo bar baz
$ python3.6 -c 'd = dict.fromkeys("foo bar baz".split()); print(hash("foo"), 
*d)'
5190466601789110813 foo bar baz

So if the OP had used Python 3.6 she would have seen the behaviour she 
expected. However, quoting 
<https://docs.python.org/dev/whatsnew/3.6.html#new-dict-implementation>

"""
The order-preserving aspect of this new [dict] implementation is considered 
an implementation detail and should not be relied upon
"""

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to