Home > Article > Backend Development > Python for 循环中 in 关键字含义是什么?
最常用的情况下,我理解,比如
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">):</span> <span class="k">print</span> <span class="n">i</span>
<code class="language-python"><span class="k">def</span> <span class="nf">fib</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="n">a</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">b</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="k">yield</span> <span class="n">a</span>
</code>
谢邀。刚看到问题以为楼主要问in是什么意思。<code class="language-python"><span class="c"># 以下代码在Python 2中运行</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
<span class="k">print</span> <span class="n">row</span>
<span class="c"># 完全等价于</span>
<span class="n">itr</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">__iter__</span><span class="p">()</span> <span class="c"># 获得新的迭代器</span>
<span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">row</span> <span class="o">=</span> <span class="n">itr</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
<span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
<span class="k">break</span>
<span class="k">print</span> <span class="n">row</span>
</code>
迭代器。
循环时, 程序先使用__iter__ (相当于iter(instance))获取具有next方法的对象, 然后通过其返回的对象, 不断调用其next方法, 直到StopIteration错误抛出.
<code class="language-python"><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">limit</span> <span class="o">=</span> <span class="mi">4</span>
<span class="bp">self</span><span class="o">.</span><span class="n">times</span> <span class="o">=</span> <span class="mi">0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">init</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">return</span> <span class="bp">self</span>
<span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">times</span> <span class="o">>=</span> <span class="bp">self</span><span class="o">.</span><span class="n">limit</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">StopIteration</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">init</span>
<span class="bp">self</span><span class="o">.</span><span class="n">times</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">init</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">return</span> <span class="n">x</span>
<span class="k">print</span> <span class="s">'A>>>>>>'</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">A</span><span class="p">():</span>
<span class="k">print</span> <span class="n">x</span>
</code>