News for hackers. Stuff that matters.
Re:Migrating to Python 3 with pleasure by StavrosK on Sunday February 04, 2018 @06:20PM (#16305537)
Dicts never had an iteration order, though. If you relied on dicts iterating in order, you were kind of asking for it.
Re:Migrating to Python 3 with pleasure by jstimpfle on Sunday February 04, 2018 @06:26PM (#16305557)
I believe newer Python versions have at least deterministic iteration order (correct me if I'm wrong), while some previous ones had a non-deterministic one (non-deterministic between mutiple invocations of the interpreter). But there is also OrderedDict which iterates in assignment order.
Re:Migrating to Python 3 with pleasure by StavrosK on Sunday February 04, 2018 @06:32PM (#16305571)
No, you're right. I believe dict orders were made deterministic (by insertion order) in 3.6. Before that, it was undefined.
Re:Migrating to Python 3 with pleasure by ubernostrum on Monday February 05, 2018 @01:26AM (#16307007)
In Python 2.7 prior to 2.7.3, or Python 3 prior to 3.3, iterating a dictionary would -- if it always contained the same set of keys -- be in a consistent order across multiple runs of the same program. This was an implementation detail not to be relied on.

Starting in 2.7.3 and 3.3, the hashing algorithm applied a per-process random salt to certain built-in types, in order to mitigate a potential denial-of-service vector by crafting dictionary keys which cause hash collisions. Unless the PYTHONHASHSEED environment variable was set to a consistent value, iteration over a dictionary was no longer accidentally consistent across runs.

In 3.6, the dictionary implementation was rewritten to drastically improve performance. As a side effect, dictionary iteration became consistent again, this time determined by insertion order. In 3.6, this consistency was an implementation detail and not to be relied on.

Beginning with 3.7, dictionary iteration order is finally guaranteed by the language, independent of implementation, and goes by insertion order.

Re:Migrating to Python 3 with pleasure by rtpg on Sunday February 04, 2018 @07:02PM (#16305666)
CPython interates in this fashion since 3.6, but it is not part of the Python spec.

Might be a bit contentious but it's not supposed to be relied upon

Re:Migrating to Python 3 with pleasure by thijsvandien on Sunday February 04, 2018 @07:14PM (#16305704)
It was recently decided that as of 3.7 it will be.

(Source: https://mail.python.org/pipermail/python-dev/2017-December/1...)

Re:Migrating to Python 3 with pleasure by dbcurtis on Sunday February 04, 2018 @11:40PM (#16306635)
Hmmmmmm.... that makes it hard for other implementations. CPython isn't the whole of the Python world. Requirements like that can be problematic for implementations like Micropython, for instance.
Re:Migrating to Python 3 with pleasure by sametmax on Monday February 05, 2018 @01:25AM (#16307003)
They asked people from the Jython, pypy and uPython community if it was ok before doing so.
Re:Migrating to Python 3 with pleasure by jsmeaton on Monday February 05, 2018 @12:04AM (#16306719)
The entire reason they decided to make it a standard rather than keep it as an implementation detail is to make things better for users. It’s not about just looking after cpython.
Re:Migrating to Python 3 with pleasure by lozenge on Monday February 05, 2018 @04:41AM (#16307715)
I suspect the reasoning was with CPython dominating, people would eventually make this assumption in their code, perhaps unintentionally, and reduce compatibility. Better to make it official.
Re:Migrating to Python 3 with pleasure by crdoconnor on Monday February 05, 2018 @04:50AM (#16307754)
The headache that having non-deterministic code causes more than makes up for that.

Among other things this makes writing tests much easier.

Re:Migrating to Python 3 with pleasure by fastball on Monday February 05, 2018 @06:47AM (#16308209)
Python dicts have always been touted as an unordered data structure though, so yeah, if you're depending on any type of order you shouldn't be.
Re:Migrating to Python 3 with pleasure by sethgecko on Monday February 05, 2018 @05:03AM (#16307811)
In 3.6 dicts are ordered (by insert order) as an implementation detail. From 3.7 on it will be a feature.
Re:Migrating to Python 3 with pleasure by vonseel on Sunday February 04, 2018 @09:51PM (#16306293)
There’s a well known YouTube video which covers all this comprehensively.

Raymond Hettinger, Modern Python Dictionaries... hopefully this is the correct one. https://youtu.be/npw4s1QTmPg

Re:Migrating to Python 3 with pleasure by kqr on Monday February 05, 2018 @01:31AM (#16307019)
Ooh, I'm no longer involved in the Python world but I love presentations by Hettinger. Thanks!