bpo-10544: Deprecate "yield" in comprehensions and generator expressi… · python/cpython@73a7e9b (original) (raw)
`` @@ -183,8 +183,21 @@ by considering each of the :keyword:for
or :keyword:if
clauses a block,
``
183
183
`nesting from left to right, and evaluating the expression to produce an element
`
184
184
`each time the innermost block is reached.
`
185
185
``
186
``
`-
Note that the comprehension is executed in a separate scope, so names assigned
`
187
``
`-
to in the target list don't "leak" into the enclosing scope.
`
``
186
`` +
However, aside from the iterable expression in the leftmost :keyword:for
clause,
``
``
187
`+
the comprehension is executed in a separate implicitly nested scope. This ensures
`
``
188
`+
that names assigned to in the target list don't "leak" into the enclosing scope.
`
``
189
+
``
190
`` +
The iterable expression in the leftmost :keyword:for
clause is evaluated
``
``
191
`+
directly in the enclosing scope and then passed as an argument to the implictly
`
``
192
`` +
nested scope. Subsequent :keyword:for
clauses and any filter condition in the
``
``
193
`` +
leftmost :keyword:for
clause cannot be evaluated in the enclosing scope as
``
``
194
`+
they may depend on the values obtained from the leftmost iterable. For example:
`
``
195
``[x*y for x in range(10) for y in range(x, x+10)]``.
``
196
+
``
197
`+
To ensure the comprehension always results in a container of the appropriate
`
``
198
type, ``yield`` and ``yield from`` expressions are prohibited in the implicitly
``
199
`` +
nested scope (in Python 3.7, such expressions emit :exc:DeprecationWarning
``
``
200
`` +
when compiled, in Python 3.8+ they will emit :exc:SyntaxError
).
``
188
201
``
189
202
`` Since Python 3.6, in an :keyword:async def
function, an :keyword:async for
``
190
203
`` clause may be used to iterate over a :term:asynchronous iterator
.
``
`` @@ -198,6 +211,13 @@ or :keyword:await
expressions it is called an
``
198
211
`suspend the execution of the coroutine function in which it appears.
`
199
212
`` See also :pep:530
.
``
200
213
``
``
214
`+
.. versionadded:: 3.6
`
``
215
`+
Asynchronous comprehensions were introduced.
`
``
216
+
``
217
`+
.. deprecated:: 3.7
`
``
218
``yield`` and ``yield from`` deprecated in the implicitly nested scope.
``
219
+
``
220
+
201
221
`.. _lists:
`
202
222
``
203
223
`List displays
`
`@@ -316,27 +336,42 @@ brackets or curly braces.
`
316
336
``
317
337
`Variables used in the generator expression are evaluated lazily when the
`
318
338
`` :meth:~generator.__next__
method is called for the generator object (in the same
``
319
``
`` -
fashion as normal generators). However, the leftmost :keyword:for
clause is
``
320
``
`-
immediately evaluated, so that an error produced by it can be seen before any
`
321
``
`-
other possible error in the code that handles the generator expression.
`
322
``
`` -
Subsequent :keyword:for
clauses cannot be evaluated immediately since they
``
323
``
may depend on the previous :keyword:`for` loop. For example: ``(x*y for x in
324
``
range(10) for y in bar(x))``.
``
339
`+
fashion as normal generators). However, the iterable expression in the
`
``
340
`` +
leftmost :keyword:for
clause is immediately evaluated, so that an error
``
``
341
`+
produced by it will be emitted at the point where the generator expression
`
``
342
`+
is defined, rather than at the point where the first value is retrieved.
`
``
343
`` +
Subsequent :keyword:for
clauses and any filter condition in the leftmost
``
``
344
`` +
:keyword:for
clause cannot be evaluated in the enclosing scope as they may
``
``
345
`+
depend on the values obtained from the leftmost iterable. For example:
`
``
346
``(x*y for x in range(10) for y in range(x, x+10))``.
325
347
``
326
348
`The parentheses can be omitted on calls with only one argument. See section
`
327
349
`` :ref:calls
for details.
``
328
350
``
``
351
`+
To avoid interfering with the expected operation of the generator expression
`
``
352
itself, ``yield`` and ``yield from`` expressions are prohibited in the
``
353
`+
implicitly defined generator (in Python 3.7, such expressions emit
`
``
354
`` +
:exc:DeprecationWarning
when compiled, in Python 3.8+ they will emit
``
``
355
`` +
:exc:SyntaxError
).
``
``
356
+
329
357
`` If a generator expression contains either :keyword:async for
``
330
358
`` clauses or :keyword:await
expressions it is called an
``
331
359
`` :dfn:asynchronous generator expression
. An asynchronous generator
``
332
360
`expression returns a new asynchronous generator object,
`
333
361
`` which is an asynchronous iterator (see :ref:async-iterators
).
``
334
362
``
``
363
`+
.. versionadded:: 3.6
`
``
364
`+
Asynchronous generator expressions were introduced.
`
``
365
+
335
366
`.. versionchanged:: 3.7
`
336
367
` Prior to Python 3.7, asynchronous generator expressions could
`
337
368
`` only appear in :keyword:async def
coroutines. Starting
``
338
369
` with 3.7, any function can use asynchronous generator expressions.
`
339
370
``
``
371
`+
.. deprecated:: 3.7
`
``
372
``yield`` and ``yield from`` deprecated in the implicitly nested scope.
``
373
+
``
374
+
340
375
`.. _yieldexpr:
`
341
376
``
342
377
`Yield expressions
`
`@@ -364,6 +399,16 @@ coroutine function to be an asynchronous generator. For example::
`
364
399
` async def agen(): # defines an asynchronous generator function (PEP 525)
`
365
400
` yield 123
`
366
401
``
``
402
Due to their side effects on the containing scope, ``yield`` expressions
``
403
`+
are not permitted as part of the implicitly defined scopes used to
`
``
404
`+
implement comprehensions and generator expressions (in Python 3.7, such
`
``
405
`` +
expressions emit :exc:DeprecationWarning
when compiled, in Python 3.8+
``
``
406
`` +
they will emit :exc:SyntaxError
)..
``
``
407
+
``
408
`+
.. deprecated:: 3.7
`
``
409
`+
Yield expressions deprecated in the implicitly nested scopes used to
`
``
410
`+
implement comprehensions and generator expressions.
`
``
411
+
367
412
`Generator functions are described below, while asynchronous generator
`
368
413
`functions are described separately in section
`
369
414
`` :ref:asynchronous-generator-functions
.
``