欢迎贵客来到小店。这铺子打新世纪初开了张,到现在,套个时髦话,得准备“奔三”了。老家伙什儿不老少,新玩意儿也换得勤。外头说Python玄乎,好似神仙法器;您得走进来瞧瞧,指不定就是些油盐柴米,就凑了个海陆空全席。
今儿开篇,先讲二十年来的镇店之宝,一张【禅】。
这卷上写的是个洋文——Python原来的都是洋文,不过在店里,咱还是说老祖宗的话。这洋文是Python祖师爷之一Tim Peters的名言,打开始就没变过。说好比那忠义堂里的“忠义”,跑堂会上的堂规,离了它,这一屋子的玩意儿就散了精气神儿。
这里先请出来,给各位爷过个目,咱再讲它高明的地方。
import this
The Zen of Python, by Tim Peters # Python 之禅
1) Beautiful is better than ugly. # 写得俊总比寒碜好
2) Explicit is better than implicit. # 写明白好过打哑谜
3) Simple is better than complex. # 简单好过复杂
4) Complex is better than complicated. # 复杂好过一团乱麻
5) Flat is better than nested. # 摊开来好过一层套一层
6) Sparse is better than dense. # 留白好过挤成团
7) Readability counts. # 让人读得明白是规矩
8) Special cases aren’t special enough to break the rules. # 这天皇老子一样大的特殊情况也不能打破
9) Although practicality beats purity. # 中用强过中看
10) Errors should never pass silently. # 但错误(Error) 也不能让他落了跑
11) Unless explicitly silenced. # 放走的也得留个印
12) In the face of ambiguity, refuse the temptation to guess. # 能动手就别瞎猜
13) There should be one– and preferably only one –obvious way to do it. # 通天大道就一条
14) Although that way may not be obvious at first unless you’re Dutch. # 哪怕你不是Dutch,不容易找着,那也只有一条
15) Now is better than never. # 动手总比不动的强
16) Although never is often better than right now. # 但是 老也不动 也强过 火烧屁股着急忙慌
17) If the implementation is hard to explain, it’s a bad idea. # 实现要是讲不利索,它就是个棒槌
18) If the implementation is easy to explain, it may be a good idea. # 实现至少得能讲利索,那才有点儿意思
19) Namespaces are one honking great idea – let’s do more of those! # 命名空间是个宝,能整多少整多少
祖师爷的意思,咱都分好了行,给标注清楚了。
没有规矩,哪成方圆。它说的就是进店上架的规矩。
想搁这店里有个好位置,这东西,得做得敞亮大气,一目了然(1~8);瑕疵(Error)的地方,都得点出来(9~11);开始做的时候,也得心里有数,手上勤快,活计清爽(13~19)
一段话把做成什么样,不好的地方怎么处理,该怎么开始做,都给讲全喽。
至于这大道理怎么落到实物上的,不急,咱们往下再看。