分类
Python编程

简单是 Python 编程的第一要则

本文是 Python 之禅特别系列的第二篇,我们将要关注其中第三与第四条原则:简单与复杂。 ——Moshe Zadka1)Moshe has been involved in the Linux community since 1998, helping in Linux “installation parties”. He has been programming Python since 1999, and has contributed to the core Python interpreter. Moshe has been a DevOps/SRE since before those terms existed, caring deeply about software reliability, build reproducibility and other such things. He has worked in companies as small as three people and as big as tens of thousands — usually some place around where software meets system administration.

Il semble que la perfection soit atteinte non quand il n’y a plus rien à ajouter, mais quand il n’y plus rien à retrancher. It seems that perfection is finally attained not when there is no longer anything to add, but when there is no longer anything to take away.

“完美并非无可增,而是不可减。”

—Antoine de Saint-Exupéry, Terre des Hommes, 1939

编程时最常有的考量是与复杂性的斗争,只想写出让旁人无从下手的繁杂代码,对每个程序员来讲都算不上难事。倘若未能触及代码的简繁取舍,那么 《Python 之禅》 就有了一角残缺。

简单胜过复杂

simple is better than complex

尚有选择余地时,应该选简单的方案。Python 少有不可为之事,这意味着设计出巴洛克风格(LCTT 译注:即夸张和不理性)的程序只为解决浅显的问题不仅有可能,甚至很简单。

正因如此,编程时应当谨记,代码的简单性是最易丢失,却最难复得的。

这意味着,在可以选用函数来表达时不要去引入额外的类;避免使用强力的第三方库往往有助于你针对迫切的问题场景设计更妥当的简短函数。不过其根本的意图,旨在让你减少对将来的盘算,而去着重解决手头的问题。

以简单和优美作为指导原则的代码相比那些想要囊括将来一切变数的,在日后要容易修改得多。

复杂胜过错综复杂

complex is better than complicated

把握用词的精确含义对于理解这条令人费解的原则是至关重要的。形容某事复杂(complex),是说它由许多部分组成,着重组成成分之多;而形容某事错综复杂(complicated),则是指其包含着差异巨大、难以预料的行为,强调的是各组成部分之间的杂乱联系。

解决困难问题时,往往没有可行的简单方案。此时,最 Python 化的策略是“自底向上(bottom-up)”地构建出简单的工具,之后将其组合用以解决该问题。

这正是对象组合(object compostition)这类技术的闪耀之处,它避免了错综复杂的继承体系,转而由独立的对象把一些方法调用传递给别的独立对象。这些对象都能独立地测试与部署,最终却可以组成一体。

“自底建造” 的另一例即是单分派泛函数(singledispatch)的使用,抛弃了错综复杂的对象之后,我们得到是简单、几乎无行为的对象以及独立的行为。


via: https://opensource.com/article/19/12/zen-python-simplicity-complexity

作者:Moshe Zadka 

选题:lujun9972 

译者:caiichenr 

校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

References   [ + ]

1.Moshe has been involved in the Linux community since 1998, helping in Linux “installation parties”. He has been programming Python since 1999, and has contributed to the core Python interpreter. Moshe has been a DevOps/SRE since before those terms existed, caring deeply about software reliability, build reproducibility and other such things. He has worked in companies as small as three people and as big as tens of thousands — usually some place around where software meets system administration.

发表评论

电子邮件地址不会被公开。 必填项已用*标注