擅长:python、mysql、java
<p>正确地说,{cdm>使用的是什么呢。例如,以下代码将不会变懒:</p>
<pre><code>print(any([slow_operation(x) for x in big_list]))
</code></pre>
<p>列表将被完全构造和计算,然后作为参数传递给<code>any</code>。在</p>
<p>另一方面,生成器是ITerable,可以根据需要计算每个项目。它们可以是<a href="https://www.python.org/dev/peps/pep-0289/" rel="nofollow noreferrer">expressions</a>,<a href="https://www.python.org/dev/peps/pep-0255/" rel="nofollow noreferrer">functions</a>,或者有时手动实现为lazy <a href="https://docs.python.org/3/tutorial/classes.html#iterators" rel="nofollow noreferrer">iterators</a>。在</p>