Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New in Python 3.7: "Dict order is guaranteed to be insertion order." #86

Open
ghost opened this issue Apr 16, 2018 · 5 comments
Open

New in Python 3.7: "Dict order is guaranteed to be insertion order." #86

ghost opened this issue Apr 16, 2018 · 5 comments

Comments

@ghost
Copy link

ghost commented Apr 16, 2018

From: https://docs.python.org/3.7/library/stdtypes.html#typesmapping

iter(dictview)

.....

Changed in version 3.7: Dict order is guaranteed to be insertion order.

We all know that as of 3.6, iterating over items in a dictionary is now done in the order in which they were inserted as an implementation detail of CPython. Apparently, as of 3.7, iterating over items in a dictionary is now done in the order in which they were inserted as a "feature" of the language.

I would like to request that this "feature" not be included in Tauthon, because it makes it more difficult to implement custom mapping classes that implement dict's API.

Pre-3.7, to implement a custom mapping class that matches dict's API, one simply needs to allow iteration of all keys/values in the mapping. As of 3.7, to implement a custom mapping class that matches dict's API one needs to allow iteration of all key/values in the mapping in the order in which they were inserted. This may be infeasible for some custom mapping classes.

My suggestion is the following:

  • Implement 3.6's new in-order dict class for the memory and performance benefits
  • Do not guarantee that keys/values are returned in insertion order

The value of being able to replace instances of dict with instances of a custom mapping class without needing to implement ordering or worrying that unimplemented ordering will cause subtle bugs outweighs the convenience of writing dict rather than OrderedDict when ordering semantics must be guaranteed.

I understand that this may be controversial. I'm saying that the Python 3 team made a bad decision by guaranteeing ordering in dictionaries. I stand by that statement. It was a bad decision that makes the language less flexible in both underlying implementation and in use and encourages users to rely on non-backwards-compatible language features. A conspiratorially minded person might suspect that breaking backwards compatibility was one of the reasons for adoption.

Thank you!

@proteasome
Copy link

insertion order, while frequently convenient, is not the same as sorted (or collated), and feels like a specific use case. i also wonder whether a dictionary added to by multiple generator/yield, functions, threads, processes might have some quirky unanticipated behavior caused by race conditions.

@torgil
Copy link

torgil commented Apr 28, 2018

From experience, I think go's choice of random iteration order could flush out some un-necessary bugs.

https://nathanleclaire.com/blog/2014/04/27/a-surprising-feature-of-golang-that-colored-me-impressed/

@vandys
Copy link

vandys commented May 29, 2018

Having implemented dict-like data structures in many, many languages over the years, I very much
agree with Mr. Atkinson that the Python 3 team made a bad call, and it adds unnecessary baggage
to arguably the most performance intensive data structure in the language. I hope Tauthon chooses
to decline to commit to this semantic.

@rec
Copy link

rec commented Oct 27, 2023

@vandys What actually happened is that the Python core team made an optimization that improved the performance of dictionaries considerably, and, almost as a side effect, preserved insertion order.

So for one Python version, 3.6, after the optimization was implemented, Python had this feature, but it wasn't necessarily guaranteed for future versions. But it worked sufficiently well that 3.7 guaranteed this feature.

Determinism is always nice, and this dropped out for free as part of an optimization, so I like it a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@rec @vandys @torgil @proteasome and others