From 3255d5530b4cb5de9b45c2fb41cd6eebb81e4e2c Mon Sep 17 00:00:00 2001 From: Florian Dahlitz Date: Wed, 20 May 2020 22:56:11 +0200 Subject: [PATCH 1/6] bpo-29981: Update Index for set, dict, and generator 'comprehensions' --- Doc/glossary.rst | 12 ++++++++++++ Doc/library/stdtypes.rst | 14 ++++++++++++++ Doc/reference/expressions.rst | 4 ++++ 3 files changed, 30 insertions(+) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index 6189cb045049c2a..e07456c2217f22f 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -304,6 +304,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 elements in a sequence and + return a dictionary with the results. ``results = {n: n ** 2 for n in + range(10)}`` generates a dictionary containing key ``n`` which mapped to + value ``n ** 2``. See :ref:`comprehensions`. + dictionary view The objects returned from :meth:`dict.keys`, :meth:`dict.values`, and :meth:`dict.items` are called dictionary views. They provide a dynamic @@ -1004,6 +1010,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 + c not in 'abc'}`` generates the set of strings with ``{'r', 'd'}``. See + :ref:`comprehensions`. + sequence An :term:`iterable` which supports efficient element access using integer indices via the :meth:`__getitem__` special method and defines a diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 4e7729c83f49a41..89f911c43a7b72c 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4114,6 +4114,12 @@ The constructors for both classes work the same: .. class:: set([iterable]) frozenset([iterable]) + Sets can be created by several means: + + * Use a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}`` + * Use a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}`` + * Use the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])`` + 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` @@ -4309,6 +4315,14 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: dict(mapping, **kwarg) dict(iterable, **kwarg) + Dictionaries can be created by several means: + + * Use a comma-separated list of ``key: value`` pairs within braces: + ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}`` + * Use a dict comprehension: ``{}``, ``{x: x ** 2 for x in range(10)}`` + * Use 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. diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 8036a491c29ab11..17adfa433dd4d06 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -162,6 +162,8 @@ ambiguities and allow common typos to pass uncaught. Displays for lists, sets and dictionaries ----------------------------------------- +.. index:: single: comprehensions + For constructing a list, a set or a dictionary Python provides special syntax called "displays", each of them in two flavors: @@ -260,6 +262,7 @@ Set displays .. index:: pair: set; display + pair: set; comprehensions object: set single: {} (curly brackets); set expression single: , (comma); expression list @@ -287,6 +290,7 @@ Dictionary displays .. index:: pair: dictionary; display + pair: dictionary; comprehensions key, datum, key/datum pair object: dictionary single: {} (curly brackets); dictionary expression From 24d507cb4c68162919d397b9b4199cc935526a63 Mon Sep 17 00:00:00 2001 From: Florian Dahlitz Date: Sat, 6 Jun 2020 09:27:41 +0200 Subject: [PATCH 2/6] Apply suggestions from @remilapeyre MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rémi Lapeyre --- Doc/glossary.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index e07456c2217f22f..f50c773f19dbe8c 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -305,9 +305,9 @@ Glossary Called a hash in Perl. dictionary comprehension - A compact way to process all or part of the elements in a sequence and + A compact way to process all or part of the elements in a iterable and return a dictionary with the results. ``results = {n: n ** 2 for n in - range(10)}`` generates a dictionary containing key ``n`` which mapped to + range(10)}`` generates a dictionary containing key ``n`` mapped to value ``n ** 2``. See :ref:`comprehensions`. dictionary view @@ -1011,9 +1011,9 @@ Glossary memory-critical application. set comprehension - A compact way to process all or part of the elements in a sequence and + A compact way to process all or part of the elements in a iterable and return a set with the results. ``results = {c for c in 'abracadabra' if - c not in 'abc'}`` generates the set of strings with ``{'r', 'd'}``. See + c not in 'abc'}`` generates the set of strings ``{'r', 'd'}``. See :ref:`comprehensions`. sequence From 28944aa88b31ccef4952ee8199a515f283a638c3 Mon Sep 17 00:00:00 2001 From: Florian Dahlitz Date: Thu, 11 Jun 2020 22:32:39 +0200 Subject: [PATCH 3/6] Apply suggestion from @akuchling --- Doc/glossary.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index f50c773f19dbe8c..9a0ebdf527304fb 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -305,7 +305,7 @@ Glossary Called a hash in Perl. dictionary comprehension - A compact way to process all or part of the elements in a iterable and + A compact way to process all or part of the elements in an iterable and return a dictionary with the results. ``results = {n: n ** 2 for n in range(10)}`` generates a dictionary containing key ``n`` mapped to value ``n ** 2``. See :ref:`comprehensions`. @@ -1011,7 +1011,7 @@ Glossary memory-critical application. set comprehension - A compact way to process all or part of the elements in a iterable and + A compact way to process all or part of the elements in an iterable and return a set with the results. ``results = {c for c in 'abracadabra' if c not in 'abc'}`` generates the set of strings ``{'r', 'd'}``. See :ref:`comprehensions`. From 4366843069a0d9ee21b6f1bc4a6a15cc467e125e Mon Sep 17 00:00:00 2001 From: Florian Dahlitz Date: Tue, 20 Oct 2020 16:50:13 +0200 Subject: [PATCH 4/6] Apply suggestion from @akuchling --- Doc/library/stdtypes.rst | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 89f911c43a7b72c..53edc4a3765fad1 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4114,18 +4114,18 @@ The constructors for both classes work the same: .. class:: set([iterable]) frozenset([iterable]) - Sets can be created by several means: - - * Use a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}`` - * Use a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}`` - * Use the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])`` - 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` objects. If *iterable* is not specified, a new empty set is returned. + Sets can be created by several means: + + * Use a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}`` + * Use a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}`` + * Use the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])`` + Instances of :class:`set` and :class:`frozenset` provide the following operations: @@ -4315,6 +4315,9 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: dict(mapping, **kwarg) dict(iterable, **kwarg) + Return a new dictionary initialized from an optional positional argument + and a possibly empty set of keyword arguments. + Dictionaries can be created by several means: * Use a comma-separated list of ``key: value`` pairs within braces: @@ -4323,9 +4326,6 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: * Use 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. - If no positional argument is given, an empty dictionary is created. If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, From e43717799ea8973057a51dcaefadb45589417c5b Mon Sep 17 00:00:00 2001 From: Andrew Kuchling Date: Tue, 20 Oct 2020 17:04:03 -0400 Subject: [PATCH 5/6] Fix alphabetical ordering --- Doc/glossary.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index 9a0ebdf527304fb..dfee6e5a257cce3 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -1010,12 +1010,6 @@ 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 an iterable and - return a set with the results. ``results = {c for c in 'abracadabra' if - c not in 'abc'}`` generates the set of strings ``{'r', 'd'}``. See - :ref:`comprehensions`. - sequence An :term:`iterable` which supports efficient element access using integer indices via the :meth:`__getitem__` special method and defines a @@ -1034,6 +1028,12 @@ Glossary interface can be registered explicitly using :func:`~abc.register`. + set comprehension + A compact way to process all or part of the elements in an iterable and + return a set with the results. ``results = {c for c in 'abracadabra' if + c not in 'abc'}`` generates the set of strings ``{'r', 'd'}``. See + :ref:`comprehensions`. + single dispatch A form of :term:`generic function` dispatch where the implementation is chosen based on the type of a single argument. From 3d0c1ec9758ad2a105f80fa312bff1c4666cf64d Mon Sep 17 00:00:00 2001 From: Andrew Kuchling Date: Tue, 20 Oct 2020 17:05:31 -0400 Subject: [PATCH 6/6] Update ACKS --- Misc/ACKS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/ACKS b/Misc/ACKS index 6511383fa25d7cd..aa112243f4edc7f 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -377,6 +377,7 @@ Brian Curtin Jason Curtis Hakan Celik Paul Dagnelie +Florian Dahlitz Lisandro Dalcin Darren Dale Andrew Dalke