擅长:python、mysql、java
<p>最令人困惑的部分应该是这一行<code>X = yield i</code>,特别是当您在生成器上调用<code>send()</code>时。实际上你只需要知道:</p>
<p>在词汇层面:
<strong><code>next()</code>等于<code>send(None)</code></strong></p>
<p>在解释器级别:
<strong><code>X = yield i</code></strong>等于下面的行(<strong>顺序问题</strong>):</p>
<pre><code>yield i
# won't continue until next() or send() is called
# and this is also the entry point of next() or send()
X = the_input_of_send
</code></pre>
<p>而且,这两行注释正是我们需要第一次调用<code>send(None)</code>的原因,因为生成器将返回<code>i</code>(yield<code>i</code>)<strong>,然后再将值赋给<code>X</code></p>