bpo-29981: Update Index for set, dict, and generator 'comprehensions'#995
bpo-29981: Update Index for set, dict, and generator 'comprehensions'#995louisom wants to merge 7 commits intopython:masterfrom louisom:doc_29981
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. |
orsenthil
left a comment
There was a problem hiding this comment.
This change looks good to me.
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 = {'foo' for _ in range(10)}`` |
There was a problem hiding this comment.
This is bad example. The result is just {'foo'}.
There was a problem hiding this comment.
See an example in tutorial.
There was a problem hiding this comment.
@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'}``. |
|
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: |
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.
Empty sets also can be created by using the constructor and set comprehension.
There was a problem hiding this comment.
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.
{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.
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.
Maybe "Using a dict comprehension"?
There was a problem hiding this comment.
Another question, so {} is a dict comprehension, or a dict constructor?
bitdancer
left a comment
There was a problem hiding this comment.
Thanks for working on this. I have some suggestions.
Doc/glossary.rst
Outdated
| 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.
"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.
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.
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.
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.
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.
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.
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.
As above, I think this would be better at the start of the section, with the constructor sentence last.
bitdancer
left a comment
There was a problem hiding this comment.
With this exception of the one typo, this looks good to me.
Doc/library/stdtypes.rst
Outdated
| .. class:: set([iterable]) | ||
| frozenset([iterable]) | ||
|
|
||
| Sets can be created by several ways:b |
There was a problem hiding this comment.
Looks like you have a stray 'b' there at the end of the sentence.
There was a problem hiding this comment.
Yes, I should self-check this one.
| 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.
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.
|
|
||
| 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 |
|
|
||
| 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.
I would use 'Use' instead of 'Using' here and rest of list
| .. class:: set([iterable]) | ||
| frozenset([iterable]) | ||
|
|
||
| Sets can be created by several ways: |
There was a problem hiding this comment.
'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.
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.
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.