擅长:python、mysql、java
<p>在lambda中,您试图创建的一行代码实际上在技术上是可行的,您只需要进一步帮助解析器:</p>
<pre><code>>>> lamyield = lambda: [(yield x) for x in range(15)]
>>> print(*lamyield())
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
</code></pre>
<p>这在列表理解中隐式地使用for循环。在理解之外使用显式的<code>while</code>循环或<code>for</code>循环是不可能的。这是因为python中的lambdas只能包含<a href="https://docs.python.org/3/reference/expressions.html" rel="noreferrer">expressions</a>,要编写显式循环,需要使用<a href="https://docs.python.org/3/reference/compound_stmts.html" rel="noreferrer">statements</a>。</p>
<p><a href="https://docs.python.org/3.7/whatsnew/3.7.html#deprecated-python-behavior" rel="noreferrer"><strong>Note: this syntax is deprecated in Python 3.7, and will raise ^{<cd3>} in Python 3.8</strong></a></p>