-
Notifications
You must be signed in to change notification settings - Fork 42
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
Comments
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. |
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/ |
Having implemented dict-like data structures in many, many languages over the years, I very much |
@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. |
From: https://docs.python.org/3.7/library/stdtypes.html#typesmapping
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 matchesdict
'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:
dict
class for the memory and performance benefitsThe 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 writingdict
rather thanOrderedDict
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!
The text was updated successfully, but these errors were encountered: