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!
命令行里敲入“python”——>"import this",会出现一段很惊艳的诗,The Zen of Python。
第一遍读,只理解了百分之五十,于是抄写下来,再把翻译抄写一遍,逐渐理解了其中的意思。
常常听说“代码之美”,可能因为段位不够,并不理解什么养的代码是“美”的,读完这首诗,好像有点懂了。
比如“Sparse is better than dense.”,间隔胜于紧凑,写函数或者双目运算符的时候,都是要加一些空格的。
比如这两段代码比较,上面的明显更加清晰明了。
function (a, b) {
return a + b;
}
function(a,b){
return a+b;
}
还有非常有趣的一个地方,在这句“Although that way may not be obvious at first unless you're Dutch.”,Dutch是丹麦人的意思,开始我猜测这里面可能是一个梗,作者在打趣丹麦人,后来看了翻译发现,“Dutch“指的是Python之父Guido。
看来真是应了那句,“In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.”
模棱两可的时候不要随意猜测。