2

不要用do while,无事读读PEP

 2 years ago
source link: https://zhuanlan.zhihu.com/p/427950406
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

不要用do while,无事读读PEP

CrackingOysters,源于编程珠玑,也是公众号

今天看到如下的问题,

do…while比while好在那里?为什么要专门设计个do…while?

突然想到之前看的Python PEP。觉得这挺有意思的,值得写一写。

PEP全称Python Enhancement Proposal,Python的加强方案。

本文可以概括为:无事读读PEP收获多!

为什么do while 不要用

因为do while的实现的目的都可以用如下的while True替换掉

while True:
    <set up code>
    if <condition not meet>:
        break
    <loop body>

而且没有什么形式的do while可以与这样的while True竞争。

从我个人的编程经历来看,的确是的。如果写do while还要思考如何写才是正确的,但是用while True这样的形式却很自然。

do…while比while好在那里?为什么要专门设计个do…while? 这个题目里面好多回答举的例子都可以用while True替换。

PEP来源:https://www.python.org/dev/peps/pep-0315/#footnote-2

一起来看看其他有用有趣的PEP

PEP 622 -- Structural Pattern Matching

PEP 572 -- Assignment Expressions

PEP 255 -- Simple Generators

PEP 525 -- Asynchronous Generators

接下来补充我阅读这些PIP的理解

编辑于 23 小时前

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK