擅长:python、mysql、java
<p>因为你每次都要打电话给发电机,重新启动。在</p>
<p>这里有一个固定版本:</p>
<pre><code>dates = gettime(nextdate)
for i in range(0, 25):
print dates.next() # note that you're not initializing it each time here
# just calling next()
</code></pre>
<p>这给了我:</p>
^{pr2}$
<p>要记住的一件重要的事情是,<code>yield</code>s实际上<strong>返回了一个生成器</strong>,当我们查看我的<code>dates</code>对象时可以看到:</p>
<pre><code>>>> dates
<generator object gettime at 0x02A05710>
</code></pre>
<p>这是您可以反复调用<code>next()</code>以获得下一个值的内容。每次执行循环时,都会创建一个新的生成器,并从中获取下一个(在本例中是第一个)值。在</p>