ホームページ >バックエンド開発 >Python チュートリアル >Python for 循环中 in 关键字含义是什么?

Python for 循环中 in 关键字含义是什么?

WBOY
WBOYオリジナル
2016-06-06 16:23:224039ブラウズ

最常用的情况下,我理解,比如

<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>

回复内容:

关键词:迭代器

简单来说,for in 语句是一个语法糖,具体是这样的:
  • 调用一个对象的 __iter__ 方法,方法会返回一个迭代器,所谓迭代器就是实现了 __next__ 方法的对象,如果一个对象本身就实现了 __next__(Python 2 中是直接 “next” 方法,没有下划线) ,可以直接返回自身。
  • 调用迭代器的 __next__ 返回迭代器中的“下一个”元素,比如说第一次调用会返回 0,第二次会返回 1,如此这般。
  • 最后没有元素了,迭代器抛出一个异常来表明自己没有元素了。for 语句会捕获这个异常并停下来。
我建议你独立写一个斐波那契的迭代器。
Python for 循环中 in 关键字含义是什么?Mac 怎么让截图变正常大小啊 QAQ

另外,还有一个销魂的东西叫做生成器,演示一下如何优雅地斐波那契:
(此后的代码为了简洁我都用 Python 3 来写,用 Python 2 能运行但是性能糟糕。)
<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是什么意思。
这个for实际上就是迭代,使用的是迭代器(Iterator)。
<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>
迭代器。

Python for 循环中 in 关键字含义是什么?
用dis转成虚拟机的指令
Python for 循环中 in 关键字含义是什么?发现就是GET_ITER, FOR_ITER之类 这都是把range(1:5)和f当成容器来看。文件f既然有行,那row自然是字符串了。 就个人理解:
in 关键字实现了一套python中的遍历协议.
  • 协议A: __iter__ + next
循环时, 程序先使用__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>
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。