@@ -183,8 +183,21 @@ by considering each of the :keyword:`for` or :keyword:`if` clauses a block,
183183nesting from left to right, and evaluating the expression to produce an element
184184each time the innermost block is reached.
185185
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 `).
188201
189202Since Python 3.6, in an :keyword: `async def ` function, an :keyword: `async for `
190203clause may be used to iterate over a :term: `asynchronous iterator `.
@@ -198,6 +211,13 @@ or :keyword:`await` expressions it is called an
198211suspend the execution of the coroutine function in which it appears.
199212See also :pep: `530 `.
200213
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+
201221.. _lists :
202222
203223List displays
@@ -316,27 +336,42 @@ brackets or curly braces.
316336
317337Variables used in the generator expression are evaluated lazily when the
318338: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)) ``.
325347
326348The parentheses can be omitted on calls with only one argument. See section
327349:ref: `calls ` for details.
328350
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+
329357If a generator expression contains either :keyword: `async for `
330358clauses or :keyword: `await ` expressions it is called an
331359:dfn: `asynchronous generator expression `. An asynchronous generator
332360expression returns a new asynchronous generator object,
333361which is an asynchronous iterator (see :ref: `async-iterators `).
334362
363+ .. versionadded :: 3.6
364+ Asynchronous generator expressions were introduced.
365+
335366.. versionchanged :: 3.7
336367 Prior to Python 3.7, asynchronous generator expressions could
337368 only appear in :keyword: `async def ` coroutines. Starting
338369 with 3.7, any function can use asynchronous generator expressions.
339370
371+ .. deprecated :: 3.7
372+ ``yield `` and ``yield from `` deprecated in the implicitly nested scope.
373+
374+
340375.. _yieldexpr :
341376
342377Yield expressions
@@ -364,6 +399,16 @@ coroutine function to be an asynchronous generator. For example::
364399 async def agen(): # defines an asynchronous generator function (PEP 525)
365400 yield 123
366401
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+
367412Generator functions are described below, while asynchronous generator
368413functions are described separately in section
369414:ref: `asynchronous-generator-functions `.
0 commit comments