@@ -217,10 +217,10 @@ generator::
217
217
for x in it:
218
218
yield from expr
219
219
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 ``)::
224
224
225
225
async def generator():
226
226
async for x in ait():
@@ -557,7 +557,8 @@ by a similar analogy::
557
557
for x in it:
558
558
out[k_expr] = v_expr
559
559
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 **
561
562
out = {}
562
563
for x in it:
563
564
out.update(expr)
@@ -577,15 +578,15 @@ expressions that involve unpacking::
577
578
yield from expr
578
579
g = generator()
579
580
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.
584
585
585
586
Alternatively, we don't need to think of the two ideas as separate; instead,
586
587
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 ``* ``::
589
590
590
591
out = []
591
592
for x in it:
@@ -595,7 +596,7 @@ Similarly, we can think of ``out = {...x... for x in it}`` as equivalent to the
595
596
following code, regardless of whether or not ``...x... `` uses ``* `` or ``** ``
596
597
or ``: ``::
597
598
598
- out = set()
599
+ out = set() # or out = {}
599
600
for x in it:
600
601
out.update({...x...})
601
602
@@ -744,45 +745,65 @@ to what is already available in Python, but support for using unpacking syntax
744
745
within comprehensions is rare. This section provides a brief summary of
745
746
support for similar syntax in a few other languages.
746
747
747
- Many languages that support comprehensions support double loops::
748
+ Many languages that support comprehensions support double loops:
749
+
750
+ .. code :: python
748
751
749
752
# python
750
753
[x for xs in [[1 ,2 ,3 ], [], [4 ,5 ]] for x in xs * 2 ]
751
754
755
+ .. code :: haskell
756
+
752
757
-- haskell
753
758
[x | xs <- [[1,2,3], [], [4,5]], x <- xs ++ xs]
754
759
760
+ .. code :: julia
761
+
755
762
# julia
756
763
[x for xs in [[1,2,3], [], [4,5]] for x in [xs; xs]]
757
764
765
+ .. code :: clojure
766
+
758
767
; clojure
759
768
(for [xs [[1 2 3] [] [4 5]] x (concat xs xs)] x)
760
769
761
770
Several other languages (even those without comprehensions) support these
762
771
operations via a built-in function/method to support flattening of nested
763
- structures::
772
+ structures:
773
+
774
+ .. code :: python
764
775
765
776
# python
766
777
list (itertools.chain(* (xs* 2 for xs in [[1 ,2 ,3 ], [], [4 ,5 ]])))
767
778
779
+ .. code :: javascript
780
+
768
781
// Javascript
769
782
[[1 ,2 ,3 ], [], [4 ,5 ]].flatMap (xs => [... xs, ... xs])
770
783
784
+ .. code :: haskell
785
+
771
786
-- haskell
772
787
concat (map (\x -> x ++ x) [[1,2,3], [], [4,5]])
773
788
789
+ .. code :: ruby
790
+
774
791
# ruby
775
792
[[1 , 2 , 3 ], [], [4 , 5 ]].flat_map {|e | e * 2 }
776
793
777
794
However, languages that support both comprehension and unpacking do not tend to
778
795
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
780
799
781
800
[xs... for xs in [[1,2,3], [], [4,5]]]
782
801
783
802
As one counterexample, support for a similar syntax was recently added to `Civet
784
803
<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
786
807
787
808
for xs of [[1 ,2 ,3 ], [], [4 ,5 ]] then ... (xs++ xs)
788
809
0 commit comments