bpo-29981: Update Index for set, dict, and generator 'comprehensions' by louisom · Pull Request #995 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Doc/glossary.rst
22 changes: 14 additions & 8 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3826,15 +3826,17 @@ another set. The :class:`frozenset` type is immutable and :term:`hashable` ---
its contents cannot be altered after it is created; it can therefore be used as
a dictionary key or as an element of another set.

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.

The constructors for both classes work the same:

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

Sets can be created by several ways:

Copy link
Copy Markdown
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.


* Using a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}``

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

* 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
Copy Markdown
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
Copy Markdown
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.


Return a new set or frozenset object whose elements are taken from
*iterable*. The elements of a set must be :term:`hashable`. To
represent sets of sets, the inner sets must be :class:`frozenset`
Expand Down Expand Up @@ -4024,14 +4026,18 @@ then they can be used interchangeably to index the same dictionary entry. (Note
however, that since computers store floating-point numbers as approximations it
is usually unwise to use them as dictionary keys.)

Dictionaries can be created by placing a comma-separated list of ``key: value``
pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor.

.. class:: dict(**kwarg)
dict(mapping, **kwarg)
dict(iterable, **kwarg)

Dictionaries can be created in several ways:

* Using a comma-separated list of ``key: value`` pairs within braces:
``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}``
* Using a dict comprehension: ``{}``, ``{x: x ** 2 for x in range(10)}``
* Using the type constructor: ``dict()``,
``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)``

Return a new dictionary initialized from an optional positional argument
and a possibly empty set of keyword arguments.

Expand Down
22 changes: 15 additions & 7 deletions Doc/reference/expressions.rst