yield 在推导式或生成器表达式中的应用是什么?
What are the applications of yield within a comprehension or generator expression?
Python 3.7 文档 mentions 表示 yield
表达式“在用于实现推导和生成器表达式的隐式嵌套范围内”已被弃用,3.8 的删除待处理:
Due to their side effects on the containing scope, yield expressions are not permitted as part of the implicitly defined scopes used to implement comprehensions and generator expressions (in Python 3.7, such expressions emit DeprecationWarning when compiled, in Python 3.8+ they will emit SyntaxError).
我试图了解此更改会影响什么(中断?),因为乍一看它解决了一个相当深奥的场景。首先在列表理解或生成器表达式中使用 yield
是否有充分的理由?
到目前为止,我想出了这些(相当荒谬的)示例,它们在 Python 3.8 中应该是非法的:
>>> list((yield i) for i in range(5))
[0, None, 1, None, 2, None, 3, None, 4, None]
>>> list([(yield i) for i in range(5)])
[0, 1, 2, 3, 4]
What are the applications of yield within a comprehension or generator
expression?
没有。
此 "feature" 已确认为 bug,正在为 python3.7 弃用,并将在 python3 中完全删除。 8,如果使用,结果为 SyntaxError
。
来自docs,
Yield expressions (both yield
and yield from
clauses) are now
deprecated in comprehensions and generator expressions (aside from the
iterable expression in the leftmost for clause). This ensures that
comprehensions always immediately return a container of the
appropriate type (rather than potentially returning a generator
iterator object), while generator expressions won’t attempt to
interleave their implicit output with the output from any explicit
yield expressions.
In Python 3.7, such expressions emit DeprecationWarning
when compiled,
in Python 3.8+ they will emit SyntaxError
. (Contributed by Serhiy
Storchaka in bpo-10544.)
Python 3.7 文档 mentions 表示 yield
表达式“在用于实现推导和生成器表达式的隐式嵌套范围内”已被弃用,3.8 的删除待处理:
Due to their side effects on the containing scope, yield expressions are not permitted as part of the implicitly defined scopes used to implement comprehensions and generator expressions (in Python 3.7, such expressions emit DeprecationWarning when compiled, in Python 3.8+ they will emit SyntaxError).
我试图了解此更改会影响什么(中断?),因为乍一看它解决了一个相当深奥的场景。首先在列表理解或生成器表达式中使用 yield
是否有充分的理由?
到目前为止,我想出了这些(相当荒谬的)示例,它们在 Python 3.8 中应该是非法的:
>>> list((yield i) for i in range(5))
[0, None, 1, None, 2, None, 3, None, 4, None]
>>> list([(yield i) for i in range(5)])
[0, 1, 2, 3, 4]
What are the applications of yield within a comprehension or generator expression?
没有。
此 "feature" 已确认为 bug,正在为 python3.7 弃用,并将在 python3 中完全删除。 8,如果使用,结果为 SyntaxError
。
来自docs,
Yield expressions (both
yield
andyield from
clauses) are now deprecated in comprehensions and generator expressions (aside from the iterable expression in the leftmost for clause). This ensures that comprehensions always immediately return a container of the appropriate type (rather than potentially returning a generator iterator object), while generator expressions won’t attempt to interleave their implicit output with the output from any explicit yield expressions.In Python 3.7, such expressions emit
DeprecationWarning
when compiled, in Python 3.8+ they will emitSyntaxError
. (Contributed by Serhiy Storchaka in bpo-10544.)