On 12/16/19 7:43 PM, Tim Peters wrote:
[Petr Viktorin email@example.com]
Here's one (very simplified and maybe biased) view of the history of dicts:
- 2.x: Dicts are unordered, please don't rely on the order.
- 3.3: Dict iteration order is now randomized. We told you not to rely
- 3.6: We now use an optimized implementation of dict that saves memory!
As a side effect it makes dicts ordered, but that's an implementation detail, please don't rely on it.
- 3.7: Oops, people are now relying on dicts being ordered. Insisting on
people not relying on it is battling windmills. Also, it's actually useful sometimes, and alternate implementations can support it pretty easily. Let's make it a language feature! (Later it turns out MicroPython can't support it easily. Tough luck.)
A very nice summary! My only quibble is as above: the "compact dict" implementation doesn't maintain insertion order naturally, _unless_ there are no deletions (which is, e.g., true of dicts constructed to pass keyword arguments). The code got hairier to maintain insertion order in the presence of mixing insertions and deletions.
Didn't some paths also get sliiiiightly slower as a result of maintaining insertion order when mixing insertions and deletions? My recollection is that that was part of the debate--not only "are we going to regret inflicting these semantics on posterity, and on other implementations?", but also "are these semantics worth the admittedly-small performance hit, in Python's most important and most-used data structure?".
Also, I don't recall anything about us resigning ourselves to explicitly maintain ordering on dicts because people were relying on it, "battling windmills", etc. Dict ordering had never been guaranteed, a lack of guarantee Python had always taken particularly seriously. Rather, we decided it was a desirable feature, and one worth pursuing even at the cost of a small loss of performance. One prominent Python core developer** wanted this feature for years, and I recall them saying something like:
Guido says, "When a programmer iterates over a dictionary and they see the keys shift around when the dictionary changes, they learn something!" To that I say--"Yes! They learn that Python is unreliable and random!"
** I won't give their name here because I fear I'm misquoting everybody involved. Apologies in advance if that's the case!