-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-29981: Update Index for set, dict, and generator 'comprehensions' #995
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
@grapherd, thanks for your PR! By analyzing the history of the files in this pull request, we identified @birkenfeld, @vadmium and @benjaminp to be potential reviewers. |
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.
This change looks good to me.
Doc/glossary.rst
Outdated
@@ -921,6 +927,11 @@ Glossary | |||
reserved for rare cases where there are large numbers of instances in a | |||
memory-critical application. | |||
|
|||
set comprehension | |||
A compact way to process all or part of the elements in a sequence and | |||
return a set with the results. ``results = {'foo' for _ in range(10)}`` |
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.
This is bad example. The result is just {'foo'}
.
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.
See an example in tutorial.
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.
@serhiy-storchaka thanks for mention, change another example for set comprehension.
It may be worth to mention set and dict comprehensions as ways of creating sets and dicts in |
@serhiy-storchaka there are already mention set and dict omprehensions in |
Doc/glossary.rst
Outdated
set comprehension | ||
A compact way to process all or part of the elements in a sequence and | ||
return a set with the results. ``results = {c for c in 'abracadabra' if | ||
c not in 'abc'`` generates a set of strings with ``{'r', 'd'}``. |
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.
This is missing }
I can't find mentions of set and dict comprehensions in A list comprehension is mentioned as the one of three ways of creating lists (in addition to the constructor and the bracket construction). But for sets and dicts only two ways of creating are mentioned (the constructor and the brace construction). |
I mistake the outcome, it had written in |
Doc/library/stdtypes.rst
Outdated
Non-empty sets (not frozensets) can be created by placing a comma-separated list | ||
of elements within braces, for example: ``{'jack', 'sjoerd'}``, in addition to the | ||
:class:`set` constructor. | ||
Non-empty sets (not frozensets) canb be created by several ways: |
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.
Typo: canb
.
Doc/library/stdtypes.rst
Outdated
Non-empty sets (not frozensets) can be created by placing a comma-separated list | ||
of elements within braces, for example: ``{'jack', 'sjoerd'}``, in addition to the | ||
:class:`set` constructor. | ||
Non-empty sets (not frozensets) canb be created by several ways: |
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.
Empty sets also can be created by using the constructor and set comprehension.
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 empty set can't created by set comprehension, {}
return a dict, I'll add up the constructor.
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.
{x for x in ()}
We can't say that the set created by a set comprehension is non-empty until we have created 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.
Great, I didn't thought that way to create a set before.
Doc/library/stdtypes.rst
Outdated
``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}`` | ||
* Using :class:`dict` constructor: ``dict()``, | ||
``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)`` | ||
* Using dict comprehension: ``{x: x ** 2 for x in range(10)}`` |
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 "Using a dict comprehension"?
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.
Another question, so {}
is a dict comprehension, or a dict
constructor?
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.
{}
is a dict display.
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 working on this. I have some suggestions.
Doc/glossary.rst
Outdated
@@ -272,6 +272,12 @@ Glossary | |||
keys can be any object with :meth:`__hash__` and :meth:`__eq__` methods. | |||
Called a hash in Perl. | |||
|
|||
dictionary comprehension | |||
A compact way to process all or part of the pair elements in a sequence |
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.
"process all or part of the pair elements in a sequence" makes no sense to me in this context. Most likely you just want "to process all or part of the items in a sequence" as with the set comprehension.
I think it would be good include a link to the reference documentation.
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
Doc/glossary.rst
Outdated
set comprehension | ||
A compact way to process all or part of the elements in a sequence and | ||
return a set with the results. ``results = {c for c in 'abracadabra' if | ||
c not in 'abc'}`` generates a set of strings with ``{'r', 'd'}``. |
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 you mean "generates the set of strings {'r', 'd'}
.
I think it would be good to include a link to the reference documentation.
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
Doc/library/stdtypes.rst
Outdated
* Using :class:`set` constructor: ``set()``, ``set('foobar')``, | ||
``set(['a', 'b', 'foo'])`` | ||
* Using a set comprehension: ``{x for x in ()}``, | ||
``{c for c in 'abracadabra' if c not in 'abc'}`` |
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.
Given that you adding this paragraph in parallel to the way it is done in list and tuple, it would be best to put it at the beginning of the section, the way it is in list and tuple. I would drop the example of creating the empty set via comprehension, it will just confuse the reader. Please also reorder the sentences so that they are in an order parallel to that used in list and tuple (ie: the constructor sentence is last). This is mostly a trivial style nit, but consistency is good :)
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 move this into class dict, not sure if you are saying this section. But it is now consistent with list
and tuple
.
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.
Remove empty set comprehension
Doc/library/stdtypes.rst
Outdated
``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}`` | ||
* Using :class:`dict` constructor: ``dict()``, | ||
``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)`` | ||
* Using a dict comprehension: ``{}``, ``{x: x ** 2 for x in range(10)}`` |
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.
As above, I think this would be better at the start of the section, with the constructor sentence last.
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
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.
With this exception of the one typo, this looks good to me.
Doc/library/stdtypes.rst
Outdated
The constructors for both classes work the same: | ||
|
||
.. class:: set([iterable]) | ||
frozenset([iterable]) | ||
|
||
Sets can be created by several ways:b |
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.
Looks like you have a stray 'b' there at the end of the sentence.
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, I should self-check this one.
@@ -272,6 +272,12 @@ Glossary | |||
keys can be any object with :meth:`__hash__` and :meth:`__eq__` methods. | |||
Called a hash in Perl. | |||
|
|||
dictionary comprehension | |||
A compact way to process all or part of the items in a sequence |
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.
A list/set/dict comprehension processes the items in an iterable.
If the entry for listcomp says 'sequence', it should be changed. I also do not like the 'all or part of' phrase. The iteration runs to completion. If one wants to not run the iteration to completion, one must use an explicit for loop. Perhaps the 'or part' refers to 'if' clauses. But that is a form of processing. If the iterable yields volatile items, they are gone.
@@ -921,6 +927,12 @@ Glossary | |||
reserved for rare cases where there are large numbers of instances in a | |||
memory-critical application. | |||
|
|||
set comprehension | |||
A compact way to process all or part of the elements in a sequence and | |||
return a set with the results. ``results = {c for c in 'abracadabra' if |
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.
Ditto
The constructors for both classes work the same: | ||
|
||
.. class:: set([iterable]) | ||
frozenset([iterable]) | ||
|
||
Sets can be created by several ways: | ||
|
||
* Using a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}`` |
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 would use 'Use' instead of 'Using' here and rest of list
The constructors for both classes work the same: | ||
|
||
.. class:: set([iterable]) | ||
frozenset([iterable]) | ||
|
||
Sets can be created by several ways: |
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.
'by several ways' is not idiomatic. Either 'by several means' or 'in several ways'. The latter is used below for dicts.
Have any of the other reviewers downloaded and applied the patch and rebuilt the doc to check the changes, especially the new index entries? |
|
||
* Using a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}`` | ||
* Using a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}`` | ||
* Using the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])`` |
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'm wondering if we should still advertise the use of set([...])
. We replaced all instances of it with set literals in the stdlib. See http://bugs.python.org/issue22823 for details.
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.
IMHO these examples are not for creating set literals. They demonstrate creating sets from iterables using the type constructor. Without these examples the user can though that {x for x in iterable}
is the only way.
To try and help move older pull requests forward, we are going through and backfilling 'awaiting' labels on pull requests that are lacking the label. Based on the current reviews, the best we can tell in an automated fashion is that a core developer requested changes to be made to this pull request. If/when the requested changes have been made, please leave a comment that says, |
Thanks @louisom for submitting a PR and @serhiy-storchaka, @orsenthil, @terryjreedy, @bitdancer, and @berkerpeksag for the reviews. To try and help move older pull requests forward, I'm closing this PR since it has been languishing for almost a year. |
No description provided.