当前位置: 移动技术网 > IT编程>脚本编程>Python > Python 的经典设计格言,格言来源于 Python 但不限于 Python

Python 的经典设计格言,格言来源于 Python 但不限于 Python

2018年10月07日  | 移动技术网IT编程  | 我要评论

the zen of python, by tim peters

beautiful is better than ugly.
explicit is better than implicit.
simple is better than complex.
complex is better than complicated.
flat is better than nested.
sparse is better than dense.
readability counts.
special cases aren’t special enough to break the rules.
although practicality beats purity.
errors should never pass silently.
unless explicitly silenced.
in the face of ambiguity, refuse the temptation to guess.
there should be one– and preferably only one –obvious way to do it.
although that way may not be obvious at first unless you’re dutch.
now is better than never.
although never is often better than *right* now.
if the implementation is hard to explain, it’s a bad idea.
if the implementation is easy to explain, it may be a good idea.
namespaces are one honking great idea — let’s do more of those!

可能你已经猜出来了,没错,每个 pythoneer 都知道的彩蛋,在交互式命令行输入 import this,会有一段 python 之禅格言,作者是内核开发者 tim peters, guido 叔的 python 语言设计指导原则浓缩为了 19 条开发哲学。

译文:

美丽优于丑陋。(python 以编写优美的代码为目标)
清楚优于含糊。(优美的代码应当是明了的,命名规范,风格相似)
简单优于复杂。(优美的代码应当是简洁的,不要有复杂的内部实现)
复杂优于繁琐。(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)
平坦优于曲折。(优美的代码应当是扁平的,不能有太多的嵌套)
宽松优于密集。(优美的代码有适当的间隔,不要奢望一行代码解决问题)
重要的是可读性。(优美的代码是可读的)
特殊的案例不足以特殊到破坏规则。(这些规则至高无上)
尽管实践可以打破真理。
错误却不可置之不理。
除非另有明确要求。(精准地捕获异常,不写 except:pass 风格的代码)
面对模棱两可,拒绝猜测。
总会有一个 —— 最好是只有一个 —— 显而易见的方式来明辨。(如果不确定,就用穷举法)
哪怕这种方式在开始的时候可能并不明显 —— 除非你是荷兰人 。(作者guido von rossum是荷兰人)
现在有比没有好。
尽管没有经常好于现在。(做也许好过不做,但不假思索就动手还不如不做,动手之前要细思量)
如果如何实现很难被解释清楚,那么这个想法就是一个坏想法。
如果如何实现可以被很好的解释,那么这是一个好想法。(方案测评标准)
命名空间就是一个非常好的想法 —— 让我们在这方面多做些工作吧!(倡导与号召)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网