Skip to content

Commit b1facf0

Browse files
authored
PEP 798: Fixes and Clarifications from Discourse Thread (#4518)
* add reference to PEP 525 for async generator semantics * fix syntax highlighting in code examples from other languages * attempt to fix incorrect examples in 'How to Teach This' * attempt to clarify some wording in 'How to Teach This' * slight wording change in 'How to Teach This' * remove extraneous comma * remove link to try to make automated checks pass
1 parent 64e89d3 commit b1facf0

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

peps/pep-0798.rst

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ generator::
217217
for x in it:
218218
yield from expr
219219

220-
Since ``yield from`` is not allowed inside of async generators, the equivalent
221-
for ``(*expr async for x in ait())``, is more like the following (though of
222-
course this new form should not define or reference the looping variable
223-
``i``)::
220+
Since ``yield from`` is not allowed inside of async generators (see the section
221+
of :pep:`525` on Asynchronous ``yield from``), the equivalent for ``(*expr
222+
async for x in ait())`` is more like the following (though of course this new
223+
form should not define or reference the looping variable ``i``)::
224224

225225
async def generator():
226226
async for x in ait():
@@ -557,7 +557,8 @@ by a similar analogy::
557557
for x in it:
558558
out[k_expr] = v_expr
559559

560-
# equivalent to out = {**expr for x in it}
560+
# equivalent to out = {**expr for x in it}, provided that expr evaluates to
561+
# a mapping that can be unpacked with **
561562
out = {}
562563
for x in it:
563564
out.update(expr)
@@ -577,15 +578,15 @@ expressions that involve unpacking::
577578
yield from expr
578579
g = generator()
579580

580-
We can then generalize from these specific examples to the idea that,
581-
wherever a non-starred comprehension/genexp would use an operator that
582-
adds a single element to a collection, the starred would instead use
583-
an operator that adds multiple elements to that collection.
581+
We can then generalize from these specific examples to the idea that, wherever
582+
a non-starred comprehension/genexp would use an operator that adds a single
583+
element to a collection, the starred would instead use an operator that adds
584+
multiple elements to that collection.
584585

585586
Alternatively, we don't need to think of the two ideas as separate; instead,
586587
with the new syntax, we can think of ``out = [...x... for x in it]`` as
587-
equivalent to the following code [#pep798-guido]_, regardless of whether or not
588-
``...x...`` uses ``*``::
588+
equivalent to the following [#pep798-guido]_ (where ``...x...`` is a stand-in
589+
for arbitrary code), regardless of whether or not ``...x...`` uses ``*``::
589590

590591
out = []
591592
for x in it:
@@ -595,7 +596,7 @@ Similarly, we can think of ``out = {...x... for x in it}`` as equivalent to the
595596
following code, regardless of whether or not ``...x...`` uses ``*`` or ``**``
596597
or ``:``::
597598

598-
out = set()
599+
out = set() # or out = {}
599600
for x in it:
600601
out.update({...x...})
601602

@@ -744,45 +745,65 @@ to what is already available in Python, but support for using unpacking syntax
744745
within comprehensions is rare. This section provides a brief summary of
745746
support for similar syntax in a few other languages.
746747

747-
Many languages that support comprehensions support double loops::
748+
Many languages that support comprehensions support double loops:
749+
750+
.. code:: python
748751
749752
# python
750753
[x for xs in [[1,2,3], [], [4,5]] for x in xs * 2]
751754
755+
.. code:: haskell
756+
752757
-- haskell
753758
[x | xs <- [[1,2,3], [], [4,5]], x <- xs ++ xs]
754759
760+
.. code:: julia
761+
755762
# julia
756763
[x for xs in [[1,2,3], [], [4,5]] for x in [xs; xs]]
757764
765+
.. code:: clojure
766+
758767
; clojure
759768
(for [xs [[1 2 3] [] [4 5]] x (concat xs xs)] x)
760769
761770
Several other languages (even those without comprehensions) support these
762771
operations via a built-in function/method to support flattening of nested
763-
structures::
772+
structures:
773+
774+
.. code:: python
764775
765776
# python
766777
list(itertools.chain(*(xs*2 for xs in [[1,2,3], [], [4,5]])))
767778
779+
.. code:: javascript
780+
768781
// Javascript
769782
[[1,2,3], [], [4,5]].flatMap(xs => [...xs, ...xs])
770783
784+
.. code:: haskell
785+
771786
-- haskell
772787
concat (map (\x -> x ++ x) [[1,2,3], [], [4,5]])
773788
789+
.. code:: ruby
790+
774791
# ruby
775792
[[1, 2, 3], [], [4, 5]].flat_map {|e| e * 2}
776793
777794
However, languages that support both comprehension and unpacking do not tend to
778795
allow unpacking within a comprehension. For example, the following expression
779-
in Julia currently leads to a syntax error::
796+
in Julia currently leads to a syntax error:
797+
798+
.. code:: julia
780799
781800
[xs... for xs in [[1,2,3], [], [4,5]]]
782801
783802
As one counterexample, support for a similar syntax was recently added to `Civet
784803
<https://civet.dev/>`_. For example, the following is a valid comprehension in
785-
Civet, making use of Javascript's ``...`` syntax for unpacking::
804+
Civet, making use of Javascript's ``...`` syntax for unpacking:
805+
806+
.. code:: javascript
786807
787808
for xs of [[1,2,3], [], [4,5]] then ...(xs++xs)
788809

0 commit comments

Comments
 (0)