Python技巧--01(Python之禅)

查看Python之禅,控制台输入

import this

输出:

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 以编写优美的代码为目标)

明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似)

简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)

复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)

扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)

间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题)

可读性很重要(优美的代码是可读的)

即使祭出实用性为理由,特例也不可违背这些规则。

不应默认包容所有错误,得由人明确地让它闭嘴!

面对太多的可能,不要尝试猜测,应该有一个(而且是唯一)直白的解决方法。

当然,找到这个方法不是件容易的事,谁叫你不是荷兰人呢!

但是,现在就做永远比不做要好。

若实现方案很难解释,那么它就不是一个好方案;反之也成立!

命名空间是个绝妙想法——现在就来共同体验和增进这些吧!

原文地址:https://www.cnblogs.com/sangyuming/p/11685404.html