mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-10544: Deprecate "yield" in comprehensions and generator expressions. (GH-4579)
The current behaviour of yield expressions inside comprehensions and generator expressions is essentially an accident of implementation - it arises implicitly from the way the compiler handles yield expressions inside nested functions and generators. Since the current behaviour wasn't deliberately designed, and is inherently confusing, we're deprecating it, with no current plans to reintroduce it. Instead, our advice will be to use a named nested generator definition for cases where this behaviour is desired.
This commit is contained in:
parent
6a89481680
commit
73a7e9b10b
6 changed files with 134 additions and 20 deletions
|
@ -1830,13 +1830,7 @@ Yield by itself yields None:
|
|||
[None]
|
||||
|
||||
|
||||
|
||||
An obscene abuse of a yield expression within a generator expression:
|
||||
|
||||
>>> list((yield 21) for i in range(4))
|
||||
[21, None, 21, None, 21, None, 21, None]
|
||||
|
||||
And a more sane, but still weird usage:
|
||||
Yield is allowed only in the outermost iterable in generator expression:
|
||||
|
||||
>>> def f(): list(i for i in [(yield 26)])
|
||||
>>> type(f())
|
||||
|
@ -2106,10 +2100,6 @@ enclosing function a generator:
|
|||
>>> type(f())
|
||||
<class 'generator'>
|
||||
|
||||
>>> def f(): x=(i for i in (yield) if (yield))
|
||||
>>> type(f())
|
||||
<class 'generator'>
|
||||
|
||||
>>> def f(d): d[(yield "a")] = d[(yield "b")] = 27
|
||||
>>> data = [1,2]
|
||||
>>> g = f(data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue