5年 Python 功力,总结了 10 个研发技巧
<img src="https://p3-sign.toutiaoimg.com/pgc-image/RwEVxNd7zcDSrM~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723893649&x-signature=0v3BIFzaWgpT4xeH49mxgWH%2Fn74%3D" style="width: 50%; margin-bottom: 20px;"><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">作者 | 写代码的明哥 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">源自</span> | Python编程时光</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RT4Gwk56bt5iOJ~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723893649&x-signature=HeLefPLLDInzb%2F2hNXsO5TeAEwo%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">怎样</span>在运行状态查看源代码</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">查看函数的源代码,<span style="color: black;">咱们</span><span style="color: black;">一般</span>会<span style="color: black;">运用</span> IDE 来完成。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例如</span>在 PyCharm 中,你<span style="color: black;">能够</span> Ctrl +鼠标点击进入函数的源代码。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">那<span style="color: black;">倘若</span><span style="color: black;">无</span> IDE 呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当<span style="color: black;">咱们</span>想<span style="color: black;">运用</span>一个函数时,<span style="color: black;">怎样</span><span style="color: black;">晓得</span>这个函数<span style="color: black;">必须</span>接收<span style="color: black;">那些</span>参数呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当<span style="color: black;">咱们</span>在<span style="color: black;">运用</span>函数时<span style="color: black;">显现</span>问题的时候,<span style="color: black;">怎样</span><span style="color: black;">经过</span>阅读源代码来排查问题所在呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此时</span>候,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span> inspect 来代替 IDE <span style="color: black;">帮忙</span>你完成这些事:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># demo.py</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import inspect</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def add(x, y):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">return x + y</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print("===================")</p>print(inspect.getsource(add))<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">运行结果如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$ python demo.py</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">===================</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def add(x, y):</p>return x + y<img src="https://p3-sign.toutiaoimg.com/pgc-image/RT7S2kzFTfre26~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723893649&x-signature=uYDCVUhKkIMg%2Buw6UBLlmunzEIc%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">怎样</span>关闭<span style="color: black;">反常</span>自动<span style="color: black;">相关</span>上下文</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当你在处理<span style="color: black;">反常</span>时,<span style="color: black;">因为</span>处理<span style="color: black;">欠妥</span><span style="color: black;">或</span>其他问题,再次抛出另一个<span style="color: black;">反常</span>时,往外抛出的<span style="color: black;">反常</span><span style="color: black;">亦</span>会携带原始的<span style="color: black;">反常</span>信息。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">就像<span style="color: black;">这般</span>子:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">try:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(1 / 0)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">except Exception as exc:</p>raise RuntimeError("Something bad happened")<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">从输出<span style="color: black;">能够</span>看到两个<span style="color: black;">反常</span>信息:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Traceback (most recent call last):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">File "demo.py", line 2, in</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(1 / 0)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ZeroDivisionError: division by zero</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">During handling of the above exception, another exception occurred:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Traceback (most recent call last):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">File "demo.py", line 4, in </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">raise RuntimeError("Something bad happened")</p>RuntimeError: Something bad happened<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>在<span style="color: black;">反常</span>处理程序或 finally 块中<span style="color: black;">诱发</span><span style="color: black;">反常</span>,默认<span style="color: black;">状况</span>下,<span style="color: black;">反常</span>机制会隐式工作会将先前的<span style="color: black;">反常</span>附加为新<span style="color: black;">反常</span>的 __context__属性。这<span style="color: black;">便是</span> Python 默认开启的自动<span style="color: black;">相关</span><span style="color: black;">反常</span>上下文。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你想自己<span style="color: black;">掌控</span>这个上下文,<span style="color: black;">能够</span>加个 from 关键字(from 语法会有个限制,<span style="color: black;">便是</span>第二个表达式<span style="color: black;">必要</span>是另一个<span style="color: black;">反常</span>类或实例。)来<span style="color: black;">显示</span>你的新<span style="color: black;">反常</span>是直接由哪个<span style="color: black;">反常</span><span style="color: black;">导致</span>的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">try:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(1 / 0)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">except Exception as exc:</p>raise RuntimeError("Something bad happened") from exc<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Traceback (most recent call last):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">File "demo.py", line 2, in</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(1 / 0)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ZeroDivisionError: division by zero</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">The above exception was the direct cause of the following exception:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Traceback (most recent call last):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">File "demo.py", line 4, in </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">raise RuntimeError("Something bad happened") from exc</p>RuntimeError: Something bad happened<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当然,你<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">经过</span> with_traceback <span style="color: black;">办法</span>为异常设置上下文__context__属性,这<span style="color: black;">亦</span>能在 traceback 更好的<span style="color: black;">表示</span><span style="color: black;">反常</span>信息。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">try:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(1 / 0)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">except Exception as exc:</p>raise RuntimeError("bad thing").with_traceback(exc)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,<span style="color: black;">倘若</span>我想彻底关闭这个自动<span style="color: black;">相关</span><span style="color: black;">反常</span>上下文的机制?有什么办法呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span><span style="color: black;">运用</span> raise...from None,从下面的例子上看,<span style="color: black;">已然</span><span style="color: black;">无</span>了原始<span style="color: black;">反常</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$ cat demo.py</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">try:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(1 / 0)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">except Exception as exc:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">raise RuntimeError("Something bad happened") from None</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$ python demo.py</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Traceback (most recent call last):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">File "demo.py", line 4, in </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">raise RuntimeError("Something bad happened") from None</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">RuntimeError: Something bad happened</p>(PythonCodingTime)<img src="https://p3-sign.toutiaoimg.com/pgc-image/RTJXJ1kBqzfCnu~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723893649&x-signature=za3dfUOS4ekK4PLXr2BLNYw8aRA%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">最快查看包搜索路径的方式</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当你<span style="color: black;">运用</span> import 导入一个包或模块时,Python 会去<span style="color: black;">有些</span>目录下<span style="color: black;">查询</span>,而这些目录是有优先级<span style="color: black;">次序</span>的,正<span style="color: black;">一般人</span>会<span style="color: black;">运用</span> sys.path 查看。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">>>> import sys</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">>>> from pprint import pprint</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">>>> pprint(sys.path)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">[,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/local/Python3.7/lib/python37.zip,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/local/Python3.7/lib/python3.7,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/local/Python3.7/lib/python3.7/lib-dynload,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/home/wangbm/.local/lib/python3.7/site-packages,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/local/Python3.7/lib/python3.7/site-packages]</p>>>><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">那有<span style="color: black;">无</span>更快的方式呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我这有一种连console 模式都<span style="color: black;">不消</span>进入的<span style="color: black;">办法</span>呢!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">你可能会想到这种,但这本质上与上面并无区别:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$ python -c "print(\n.join(__import__(sys).path))"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/lib/python2.7/site-packages/pip-18.1-py2.7.egg</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/lib/python2.7/site-packages/redis-3.0.1-py2.7.egg</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/lib64/python27.zip</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/lib64/python2.7</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/lib64/python2.7/plat-linux2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/lib64/python2.7/lib-tk</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/lib64/python2.7/lib-old</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/lib64/python2.7/lib-dynload</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/home/wangbm/.local/lib/python2.7/site-packages</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/lib64/python2.7/site-packages</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/lib64/python2.7/site-packages/gtk-2.0</p>/usr/lib/python2.7/site-packages<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>我要介绍的是比上面两种都方便得多的<span style="color: black;">办法</span>,一行命令<span style="color: black;">就可</span><span style="color: black;">处理</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$ python3 -m site</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">sys.path = [</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/home/wangbm,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/local/Python3.7/lib/python37.zip,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/local/Python3.7/lib/python3.7,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/local/Python3.7/lib/python3.7/lib-dynload,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/home/wangbm/.local/lib/python3.7/site-packages,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/usr/local/Python3.7/lib/python3.7/site-packages,</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">]</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">USER_BASE: /home/wangbm/.local (exists)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">USER_SITE: /home/wangbm/.local/lib/python3.7/site-packages (exists)</p>ENABLE_USER_SITE: True<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">从输出你<span style="color: black;">能够</span><span style="color: black;">发掘</span>,这个列的路径会比 sys.path 更全,它<span style="color: black;">包括</span>了用户环境的目录。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RTJXJ7YR5xGDl~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723893649&x-signature=WrwxNEeLGRJGRP%2FSArUNWTnrpn8%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">将嵌套 for 循环写成单行</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>经常会如下这种嵌套的 for 循环代码:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">list1 = range(1,3)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">list2 = range(4,6)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">list3 = range(7,9)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">for item1 in list1:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">for item2 in list2:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">for item3 in list3:</p>print(item1+item2+item3)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>仅仅是三个 for 循环,在<span style="color: black;">实质</span>编码中,有可能会有更层。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这般</span>的代码,可读性非常的差,<span style="color: black;">非常多</span>人不想这么写,可又<span style="color: black;">无</span>更好的写法。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>介绍一种我常用的写法,<span style="color: black;">运用</span> itertools 这个库来实现更优雅易读的代码。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">from itertools import product</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">list1 = range(1,3)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">list2 = range(4,6)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">list3 = range(7,9)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">for item1,item2,item3 in product(list1, list2, list3):</p>print(item1+item2+item3)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$ python demo.py</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">12</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">13</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">13</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">14</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">13</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">14</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">14</p>15<img src="https://p3-sign.toutiaoimg.com/pgc-image/RTJXJ7uHXU5GZc~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723893649&x-signature=PRpFZF7%2BAGaCmCKRcZ2zrXu8MIs%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">怎样</span><span style="color: black;">运用</span> print 输出日志</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">初学者<span style="color: black;">爱好</span><span style="color: black;">运用</span> print 来调试代码,并记录程序运行过程。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span> print 只会将内容输出到终端上,<span style="color: black;">不可</span>持久化到日志文件中,并<span style="color: black;">有害</span>于问题的排查。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你热衷于<span style="color: black;">运用</span> print 来调试代码(虽然这并不是最佳做法),记录程序运行过程,<span style="color: black;">那样</span>下面介绍的这个 print 用法,可能会对你有用。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python 3 中的 print <span style="color: black;">做为</span>一个函数,<span style="color: black;">因为</span><span style="color: black;">能够</span>接收<span style="color: black;">更加多</span>的参数,<span style="color: black;">因此</span>功能变为更加强大,指定<span style="color: black;">有些</span>参数<span style="color: black;">能够</span>将 print 的内容输出到日志文件中。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">>>> with open(test.log, mode=w) as f:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">... print(hello, python, file=f, flush=True)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">>>> exit</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$ cat test.log</p>hello, python<img src="https://p3-sign.toutiaoimg.com/pgc-image/RTLSNam5ZxLDlM~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723893649&x-signature=d%2BCrtqOsLdCsAXjtLFcesQI1CAU%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">怎样</span>快速计算函数运行时间</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">计算一个函数的运行时间,你可能会<span style="color: black;">这般</span>子做:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import time</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">start = time.time</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># run the function</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">end = time.time</p>print(end-start)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">你<span style="color: black;">瞧瞧</span>你为了计算函数运行时间,写了几行代码了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有<span style="color: black;">无</span>一种<span style="color: black;">办法</span><span style="color: black;">能够</span>更方便的计算这个运行时间呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有一个内置模块叫 timeit,<span style="color: black;">运用</span>它,只用一行代码<span style="color: black;">就可</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import time</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import timeit</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def run_sleep(second):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(second)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">time.sleep(second)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># 只用这一行</p>print(timeit.timeit(lambda :run_sleep(2), number=5))<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">运行结果如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2</p>10.020059824<img src="https://p3-sign.toutiaoimg.com/pgc-image/RTRIuPNI7JPB03~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723893649&x-signature=m4d1LVsik%2FviDETE1p7f%2BpTwNeA%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">利用自带的缓存机制提<span style="color: black;">有效</span>率</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">缓存是一种将定量数据加以<span style="color: black;">保留</span>,以备迎合后续获取需求的处理方式,旨在加快数据获取的速度。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">数据的生成过程可能<span style="color: black;">必须</span>经过计算,规整,远程获取等操作,<span style="color: black;">倘若</span>是同一份数据<span style="color: black;">必须</span>多次<span style="color: black;">运用</span>,每次都重新生成会大大浪费时间。<span style="color: black;">因此</span>,<span style="color: black;">倘若</span>将计算<span style="color: black;">或</span>远程请求等操作<span style="color: black;">得到</span>的数据缓存下来,会加快后续的数据获取需求。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为了实现这个需求,Python 3.2 + 中给<span style="color: black;">咱们</span><span style="color: black;">供给</span>了一个机制,<span style="color: black;">能够</span>很方便的实现,而不<span style="color: black;">必须</span>你去写<span style="color: black;">这般</span>的<span style="color: black;">规律</span>代码。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这个机制实现于 functool 模块中的 lru_cache 装饰器。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">@functools.lru_cache(maxsize=None, typed=False)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">参数<span style="color: black;">诠释</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">maxsize:最多<span style="color: black;">能够</span>缓存多少个此函数的调用结果,<span style="color: black;">倘若</span>为None,则无限制,设置为 2 的幂时,性能最佳;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">typed:若为 True,则<span style="color: black;">区别</span>参数类型的调用将分别缓存。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">举个例子:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">from functools import lru_cache</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">@lru_cache(None)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def add(x, y):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print("calculating: %s + %s" % (x, y))</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">return x + y</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(add(1, 2))</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(add(1, 2))</p>print(add(2, 3))<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出如下,<span style="color: black;">能够</span>看到第二次调用并<span style="color: black;">无</span>真正的执行函数体,而是直接返回缓存里的结果:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">calculating: 1 + 2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">calculating: 2 + 3</p>5<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面这个是经典的斐波那契数列,当你指定的 n <span style="color: black;">很强</span>时,会存在<span style="color: black;">海量</span>的重复计算:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def fib(n):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if n < 2:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">return n</p>return fib(n - 2) + fib(n - 1)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第六点介绍的 timeit,<span style="color: black;">此刻</span><span style="color: black;">能够</span>用它来测试一下到底<span style="color: black;">能够</span><span style="color: black;">加强</span>多少的效率。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不<span style="color: black;">运用</span> lru_cache 的<span style="color: black;">状况</span>下,运行时间 31 秒。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import timeit</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def fib(n):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if n < 2:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">return n</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">return fib(n - 2) + fib(n - 1)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(timeit.timeit(lambda :fib(40), number=1))</p># output: 31.2725698948<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因为</span><span style="color: black;">运用</span>了 lru_cache 后,运行速度实在太快了,<span style="color: black;">因此</span>我将 n 值由 30 调到 500,可即使是<span style="color: black;">这般</span>,运行时间<span style="color: black;">亦</span>才 0.0004 秒。<span style="color: black;">加强</span>速度非常<span style="color: black;">明显</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import timeit</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">from functools import lru_cache</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">@lru_cache(None)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def fib(n):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if n < 2:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">return n</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">return fib(n - 2) + fib(n - 1)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(timeit.timeit(lambda :fib(500), number=1))</p># output: 0.0004921059880871326<img src="https://p26-sign.toutiaoimg.com/pgc-image/RTRIuPaahjHjS~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723893649&x-signature=ycRyU4St6oSjCDBWMBdWXyrMaS4%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">在程序退出前执行代码的技巧</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span> atexit 这个内置模块,<span style="color: black;">能够</span>很方便的注册退出函数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不管你在哪个<span style="color: black;">地区</span><span style="color: black;">引起</span>程序崩溃,都会执行<span style="color: black;">哪些</span>你注册过的函数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例如下:</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/S1Q6Bjp8ePrUJm~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723893649&x-signature=AH%2Fz6ttrNvjvRpVwZW1VinbdkWo%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>clean函数有参数,<span style="color: black;">那样</span>你<span style="color: black;">能够</span><span style="color: black;">不消</span>装饰器,而是直接调用atexit.register(clean_1, 参数1, 参数2, 参数3=xxx)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">可能你有其他<span style="color: black;">办法</span><span style="color: black;">能够</span>处理这种需求,但肯定比上不<span style="color: black;">运用</span> atexit 来得优雅,来得方便,并且它很容易扩展。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span><span style="color: black;">运用</span>atexit 仍然有<span style="color: black;">有些</span>局限性,<span style="color: black;">例如</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>程序是被你<span style="color: black;">无</span>处理过的系统信号杀死的,<span style="color: black;">那样</span>注册的函数<span style="color: black;">没法</span>正常执行。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span><span style="color: black;">出现</span>了严重的 Python 内部错误,你注册的函数<span style="color: black;">没法</span>正常执行。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你手动调用了os._exit,你注册的函数<span style="color: black;">没法</span>正常执行。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RTYlVXmICmMrFE~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723893649&x-signature=Ug6ZX4D1Yij3VT2a%2FFAVU3ZidBc%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">实现类似 defer 的延迟调用</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在 Golang 中有一种延迟调用的机制,关键字是 defer,例如下面的示例:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import "fmt"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">func myfunc {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">fmt.Println("B")</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">func main {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">defer myfunc</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">fmt.Println("A")</p>}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出如下,myfunc 的调用会在函数返回前一步完成,即使你将 myfunc 的调用写在函数的<span style="color: black;">第1</span>行,这<span style="color: black;">便是</span>延迟调用。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">A</p>B<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span>在 Python 中否有这种机制呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当然<span style="color: black;">亦</span>有,只<span style="color: black;">不外</span>并<span style="color: black;">无</span> Golang 这种简便。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在 Python <span style="color: black;">能够</span><span style="color: black;">运用</span>上下文管理器达到这种效果:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">import contextlib</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def callback:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(B)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">with contextlib.ExitStack as stack:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">stack.callback(callback)</p>print(A)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">A</p>B<img src="https://p3-sign.toutiaoimg.com/pgc-image/RTYlVY98d1MEiD~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723893649&x-signature=I8GHh9%2FVQBvh78Jba63WwFo%2FJVQ%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">怎样</span>流式读取数G超大文件</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span> with...open... <span style="color: black;">能够</span>从一个文件中读取数据,这是所有 Python <span style="color: black;">研发</span>者都非常<span style="color: black;">熟练</span>的操作。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span><span style="color: black;">倘若</span>你<span style="color: black;">运用</span><span style="color: black;">欠妥</span>,<span style="color: black;">亦</span>会带来很大的麻烦。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例如</span>当你<span style="color: black;">运用</span>了 read 函数,其实 Python 会将文件的内容一次性的<span style="color: black;">所有</span>载入内存中,<span style="color: black;">倘若</span>文件有 10 个G<span style="color: black;">乃至</span><span style="color: black;">更加多</span>,<span style="color: black;">那样</span>你的电脑就要消耗的内存非常巨大。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># 一次性读取</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">with open("big_file.txt", "r") as fp:</p>content = fp.read<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span>这个问题,你<span style="color: black;">亦</span>许会想到<span style="color: black;">运用</span> readline 去做一个生成器来逐行返回。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def read_from_file(filename):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">with open(filename, "r") as fp:</p>yield fp.readline<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">可<span style="color: black;">倘若</span>这个文件内容就一行呢,一行就 10个G,其实你还是会一次性读取<span style="color: black;">所有</span>内容。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最优雅的<span style="color: black;">处理</span><span style="color: black;">办法</span>是,在<span style="color: black;">运用</span> read <span style="color: black;">办法</span>时,指定每次只读取固定<span style="color: black;">体积</span>的内容,<span style="color: black;">例如</span>下面的代码中,每次只读取 8kb 返回。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def read_from_file(filename, block_size = 1024 * 8):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">with open(filename, "r") as fp:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">while True:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">chunk = fp.read(block_size)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if not chunk:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">break</p>yield chunk<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上面的代码,功能上<span style="color: black;">已然</span><span style="color: black;">无</span>问题了,<span style="color: black;">然则</span>代码看起来代码还是有些臃肿。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">借助偏函数和 iter 函数<span style="color: black;">能够</span>优化一下代码:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">from functools import partial</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">def read_from_file(filename, block_size = 1024 * 8):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">with open(filename, "r") as fp:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">for chunk in iter(partial(fp.read, block_size), ""):</p>yield chunk<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">☞架构的腐化是必然的!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">☞腾讯云 AI 在新基建<span style="color: black;">行业</span>下一盘什么大棋</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">☞张一鸣是<span style="color: black;">怎样</span>练就字节跳动的</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">☞性能超越最新序列<span style="color: black;">举荐</span>模型,华为诺亚方舟提出记忆<span style="color: black;">加强</span>的图神经网络</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">☞DevOps 在移动应用程序<span style="color: black;">研发</span>中扮演什么角色?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">☞稳定币经济:十大稳定币简史</p>
大势所趋,用于讽刺一些制作目的就是为了跟风玩梗,博取眼球的作品。
页:
[1]