You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This roughly corresponds to defining and calling::
796
+
At run time, this roughly corresponds to defining and calling::
781
797
782
798
def get_a_names(iterable):
783
799
result = []
@@ -789,8 +805,7 @@ This roughly corresponds to defining and calling::
789
805
790
806
get_a_names([array, math])
791
807
792
-
In this case, and in the simpler cases in the previous sections,
793
-
the elements of the new container are those that would be produced by
808
+
The elements of the new container are those that would be produced by
794
809
considering each of the :keyword:`!for` or :keyword:`!if` clauses a block,
795
810
nesting from left to right, and evaluating the expression to produce an
796
811
element (or dictionary entry) each time the innermost block is reached.
@@ -802,7 +817,7 @@ the enclosing scope.
802
817
For example::
803
818
804
819
>>> x = 'old value'
805
-
>>> [x**2 for x in range(10)]
820
+
>>> [x**2 for x in range(10)] # this `x` is local to the comprehension
806
821
>>> x
807
822
'old value'
808
823
@@ -814,6 +829,9 @@ Subsequent :keyword:`!for` clauses and any filter condition in the
814
829
leftmost :keyword:`!for` clause cannot be evaluated in the enclosing scope as
815
830
they may depend on the values obtained from the leftmost iterable.
816
831
832
+
TODO: PEP 572:
833
+
..due to a limitation in CPython’s symbol table analysis process, the reference implementation raises SyntaxError for all uses of named expressions inside comprehension iterable expressions, rather than only raising them when the named expression target conflicts with one of the iteration variables in the comprehension. This could be revisited given sufficiently compelling examples, but the extra complexity needed to implement the more selective restriction doesn’t seem worthwhile for purely hypothetical use cases.
834
+
817
835
To ensure the comprehension always results in a container of the appropriate
818
836
type, ``yield`` and ``yield from`` expressions are prohibited in the implicitly
819
837
nested scope.
@@ -837,7 +855,7 @@ This is often used for "flattening" lists, for example::
837
855
>>> [*people for people in lists_of_people]
838
856
['Petr', 'Blaise', 'Jarka', 'Salim', 'Bartosz']
839
857
840
-
This comprehension roughly corresponds to::
858
+
At run time, this comprehension roughly corresponds to::
841
859
842
860
def flatten_names(lists_of_people):
843
861
result = []
@@ -902,6 +920,8 @@ execution of the coroutine function in which it appears.
0 commit comments