Skip to content

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

Closed
wants to merge 7 commits into from
Closed

Conversation

louisom
Copy link
Contributor

@louisom louisom commented Apr 5, 2017

No description provided.

@mention-bot
Copy link

@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.

Copy link
Member

@orsenthil orsenthil left a 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)}``
Copy link
Member

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'}.

Copy link
Member

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.

Copy link
Contributor Author

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.

@serhiy-storchaka
Copy link
Member

It may be worth to mention set and dict comprehensions as ways of creating sets and dicts in stdtypes.rst.

@louisom
Copy link
Contributor Author

louisom commented Apr 10, 2017

@serhiy-storchaka there are already mention set and dict omprehensions in stdtypes.rst, but not like link comprehension using one section.

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'}``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing }

@serhiy-storchaka
Copy link
Member

I can't find mentions of set and dict comprehensions in stdtypes.rst.

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).

@louisom
Copy link
Contributor Author

louisom commented Apr 10, 2017

I mistake the outcome, it had written in tutorial/datastructure.rst, not stdtypes.rst

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: canb.

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:
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

``{'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)}``
Copy link
Member

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"?

Copy link
Contributor Author

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{} is a dict display.

Copy link
Member

@bitdancer bitdancer left a 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
Copy link
Member

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.

Copy link
Contributor Author

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'}``.
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

* 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'}``
Copy link
Member

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 :)

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty set comprehension

``{'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)}``
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Member

@bitdancer bitdancer left a 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.

The constructors for both classes work the same:

.. class:: set([iterable])
frozenset([iterable])

Sets can be created by several ways:b
Copy link
Member

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.

Copy link
Contributor Author

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.

@Mariatta Mariatta added docs Documentation in the Doc dir needs backport to 3.6 labels Jun 7, 2017
@@ -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
Copy link
Member

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
Copy link
Member

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'}``
Copy link
Member

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:
Copy link
Member

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.

@terryjreedy
Copy link
Member

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'])``
Copy link
Member

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.

Copy link
Member

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.

@brettcannon
Copy link
Member

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, I have made the requested changes; please review again. That will trigger a bot to flag this pull request as ready for a follow-up review.

@willingc
Copy link
Contributor

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.

@willingc willingc closed this May 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting changes docs Documentation in the Doc dir needs backport to 3.9 only security fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.