-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-32337: Update tutorial and documentation related with dict order. #4973
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
Conversation
e9c06d0
to
3936c37
Compare
I'm voting AGAINST this. order in |
@socketpair order is guaranteed in 3.7. And @rhettinger is proposing to deprecate |
Doc/tutorial/datastructures.rst
Outdated
@@ -497,7 +497,8 @@ You can't use lists as keys, since lists can be modified in place using index | |||
assignments, slice assignments, or methods like :meth:`append` and | |||
:meth:`extend`. | |||
|
|||
It is best to think of a dictionary as an unordered set of *key: value* pairs, | |||
Dictionaries are guaranteed to retain insertion order. | |||
It is best to think of a dictionary as an ordered set of *key: value* pairs, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dictionary as an ordered set
can be misindestood as ordered by key, intead of ordered by insertion order.
It best to write here, that dict will be enumerated (.keys(), items(), .values()) in order of insertion. And also specify "not ordered by key".
C++ has orderdmap and unorderedmap. Orderedmap orders by key.
Also, in some natural languages, "ordered" and "sorted" are the same :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t know if other languages are relevant here. If Python docs make a clear explanation of sorted and ordered then things should not be confusing.
Doc/tutorial/datastructures.rst
Outdated
@@ -510,7 +511,7 @@ value associated with that key is forgotten. It is an error to extract a value | |||
using a non-existent key. | |||
|
|||
Performing ``list(d.keys())`` on a dictionary returns a list of all the keys | |||
used in the dictionary, in arbitrary order (if you want it sorted, just use | |||
used in the dictionary, in insertion order (if you want it sorted, just use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add example how to "permanently" sort a dict ? like:
xxx = dict(sorted(xxx.items(), key=operator.itemgetter(0)))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s make sense, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. This is tutorial. We should focus about very important part.
Previously, "unordered" was important for new users to avoid pitfall.
On the other hand, "preserving insertion order" is not so important.
Lib/test/test_dict.py
Outdated
@@ -1176,6 +1176,48 @@ def iter_and_mutate(): | |||
|
|||
self.assertRaises(RuntimeError, iter_and_mutate) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to test order for dict comprehensions ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thanks!
a69a5e7
to
73b6e50
Compare
Doc/tutorial/datastructures.rst
Outdated
@@ -497,7 +497,9 @@ You can't use lists as keys, since lists can be modified in place using index | |||
assignments, slice assignments, or methods like :meth:`append` and | |||
:meth:`extend`. | |||
|
|||
It is best to think of a dictionary as an unordered set of *key: value* pairs, | |||
Dictionaries are guaranteed to retain insertion order, not ordered by key. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just remove unordered
, and don't mention about ordering here.
While dict preserves insertion order, it's not so important in "What is dict?".
For example, dict.__eq__
ignores order.
Done. Thanks methane for your explanation. |
I feel NEWS entry is too short. |
Oh sorry, I did't saw it. That solution is great, and I am going to remove the tests in this PR. And I am wondering is there necessary to keep the "test_copy_ordering" and 'test_comprehension_ordering' here? Since there have no tests for them. |
I'm OK for document body, but I think someone can write more good commit message and NEWS entry. (Sorry, I'm not good English writer) This PR updates only tutorial. But I think other commits will be made before 3.7 final can |
Since we are editing this part, I have a question why do we use |
Misc/ACKS
Outdated
@@ -1788,3 +1788,4 @@ Jelle Zijlstra | |||
Gennadiy Zlobin | |||
Doug Zongker | |||
Peter Åstrand | |||
Shang Hui |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you move this line with other S or H names? (It’s loosely sorted by last name, for names that have a concept of last name)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will do it.
Thanks zhangyangyu and merwok, I have made a update as your suggestions. |
It's just my question. I am not sure which is actually wanted. But at least if you change it, the footprint link is not needed either. |
I think that people may use I don’t know if the keys method pre-dates the iteration protocol, but given that keys was never deprecated, to me it’s an indication that it’s fine if people use it, even if it’s technically redundant. Here in the tutorial, I would go for not changing it. |
But tutorial should teach preferred way, not redundant way? |
…dict is now guaranteed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Doc/library/collections.rst
Outdated
@@ -224,7 +224,7 @@ For example:: | |||
.. class:: Counter([iterable-or-mapping]) | |||
|
|||
A :class:`Counter` is a :class:`dict` subclass for counting hashable objects. | |||
It is an unordered collection where elements are stored as dictionary keys | |||
It is a collection where elements are stored as dictionary keys |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found more occurrences of «arbitrary order» and usage of sorted
to make examples deterministic in the rest of the file. I think careful thought should be given to the guarantees given in this file: it may be easy to say that elements
returns in insertion order, but most_common
may need to stay under-defined.
If you could get the attention of @rhettinger on the bug tracker to see what’s the best thing to do here, or possibly to not touch this file in this PR, that would be best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for you great review, but I am not very understand your meaning about:
"but most_common may need to stary under-defined.".
Could you explain it a little more?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the typo!
The basic idea is that if we guarantee that all these methods return things in insertion order now, we may preclude future implementation changes or optimizations. Each method needs to be checked to see if it’s useful to change the doc that says «unordered» (if it’s accurate now) or not (if we don’t want to guarantee it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@merwok How about creating another pull request about it?
This pull request is too long discussed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that if a file is updated, it should be updated exhaustively, not add more ambiguity than was before. But I don’t want to block the updates to the tutorial, so feel free to merge when you are satisfied.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase And if you don't make the requested changes, you will be poked with soft cushions! |
I have made the requested changes; please review again |
Thanks for making the requested changes! @zhangyangyu, @JulienPalard, @merwok: please review the changes made to this pull request. |
Document somewhere that two dicts with same contents, but different order of key-value pairs are equal. i.e. It is not obvious, and I needed to check that before use. |
@socketpair Dict equality is already documented to not rely on order. |
@socketpair See #5001 |
Let's merge this as-is. We discussed long enough. |
Thanks @shangdahao for the PR, and @methane for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7. |
(cherry picked from commit dfbbbf1) Co-authored-by: hui shang <[email protected]>
GH-6368 is a backport of this pull request to the 3.7 branch. |
(cherry picked from commit dfbbbf1) Co-authored-by: hui shang <[email protected]>
https://bugs.python.org/issue32337