首頁  >  文章  >  後端開發  >  Python 的哪些特性或用法让你相见恨晚?

Python 的哪些特性或用法让你相见恨晚?

WBOY
WBOY原創
2016-06-06 16:23:501468瀏覽

RT。最好附上实例或伪代码,方便大家交流哈!

来自Quora的相同问题:Python (programming language): What are the Python features you wish you'd known earlier?

回复内容:

Hidden features of Python

比较符连写:

<code class="language-python"><span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="mi">5</span>
<span class="o">>>></span> <span class="mi">1</span> <span class="o"> <span class="n">x</span> <span class="o"> <span class="mi">10</span>
<span class="bp">True</span>
<span class="o">>>></span> <span class="mi">10</span> <span class="o">></span> <span class="n">x</span> <span class="o"> <span class="mi">9</span>
<span class="bp">True</span>
</span></span></span></code>
  1. 测试程序执行的时间
    <code class="language-text">import time
    class Timer:
    
        def __enter__(self):
            self.start = time.clock()
            return self
    
        def __exit__(self,*args):
            self.end = time.clock()
            self.interval = self.end-self.start
    
    
    with Timer() as t:
        dosomesuch()
    print t.interval
    </code>
    x,y=y,x dir()
    还有列表解析式 [i*2 for i in range(100)] 让我们大喊三声:
    递归+yield 真好用啊
    递归+yield 真好用啊
    递归+yield 真好用啊

    这绝对是解决一些难题的专用利器.我用它来实现自定义的文件夹遍历函数..对比os.walk函数,我可以在遍历时进行任何操作,灵活许多.

    我用它实现了嵌套字典的漂亮输出,在未理解递归之前,我不敢想象自己能解决这种问题.上例子:
    <code class="language-python"><span class="k">def</span> <span class="nf">superPrint</span><span class="p">(</span><span class="n">inidic</span><span class="o">=</span><span class="p">{},</span><span class="n">indent</span><span class="o">=</span><span class="nb">chr</span><span class="p">(</span><span class="mi">32</span><span class="p">)):</span>
        <span class="n">length</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">inidic</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">d</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">inidic</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
            <span class="c">#if the k or v is string object,add ' to both sides</span>
            <span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="o">=</span><span class="p">[</span><span class="s">"'</span><span class="si">%s</span><span class="s">'"</span><span class="o">%</span><span class="n">x</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,(</span><span class="nb">str</span><span class="p">,</span><span class="nb">unicode</span><span class="p">))</span> <span class="k">else</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">d</span><span class="p">]</span>
            <span class="c">#if the v is dict object,recurse across v and return a string</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="nb">dict</span><span class="p">):</span>
                <span class="n">v</span><span class="o">=</span><span class="s">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">superPrint</span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="n">indent</span><span class="o">+</span><span class="nb">chr</span><span class="p">(</span><span class="mi">32</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">k</span><span class="p">))</span><span class="o">+</span><span class="mi">3</span><span class="p">)))</span>
            <span class="k">if</span> <span class="n">length</span><span class="o">==</span><span class="mi">1</span><span class="p">:</span>
                <span class="k">yield</span> <span class="s">"{</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">}"</span><span class="o">%</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">i</span><span class="o">==</span><span class="mi">0</span><span class="p">:</span>
                <span class="k">yield</span> <span class="s">"{</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">,</span><span class="se">\n</span><span class="s">"</span><span class="o">%</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">i</span><span class="o">==</span><span class="n">length</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="k">yield</span> <span class="s">"</span><span class="si">%s%s</span><span class="s">: </span><span class="si">%s</span><span class="s">}"</span><span class="o">%</span><span class="p">(</span><span class="n">indent</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">yield</span> <span class="s">"</span><span class="si">%s%s</span><span class="s">: </span><span class="si">%s</span><span class="s">,</span><span class="se">\n</span><span class="s">"</span><span class="o">%</span><span class="p">(</span><span class="n">indent</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span>
    </code>
    比较喜欢map ,reduce,filter省去了很多废话有没有,还有list comprehension。。。
    还有高阶函数,闭包。。
    另外么,getattr ,hasattr函数吧。。。
    with as 结构
    in关键字 也很省事。。


    其实这些特性别的语言也有,但是第一次见是在python中。。 python 的元组 、序列 、字典 数据结构及其容易操作,切片很实用。
    还有Python有很多友好方便的语法糖。
    <code class="language-python"><span class="n">help</span><span class="p">(</span><span class="n">something</span><span class="p">)</span>
    </code>
    果断是缩进。。
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn