Zen of Python(Python的19条哲学)

The Zen of Python

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就会显示上面的诗歌:

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px }
span.s1 { }

>>> 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!

中文翻译:

优美胜于丑陋

明了胜于晦涩

简单胜于复杂

复杂胜于杂乱

扁平胜于嵌套

间隔胜于紧凑

可读性很重要

特例不足以特殊到违背这些原则

不要忽视错误,除非程序需要这样做

面对模棱两可,拒绝猜测

解决问题最直接的方法应该有一种,最好只有一种

可能这种方法一开始不够直接,因为你不是范罗苏姆

做也许好过不做,但不想就做还不如不做

如果方案难以描述明白,那么一定是个糟糕的方案

如果容易描述,那么可能是个好方案

命名空间是一种绝妙的理念,多加利用

时间: 2024-10-06 13:16:09

Zen of Python(Python的19条哲学)的相关文章

数学之路-python计算实战(19)-机器视觉-卷积滤波

filter2D Convolves an image with the kernel. C++: void filter2D(InputArray src, OutputArray dst, int ddepth, InputArraykernel, Point anchor=Point(-1,-1), double delta=0, int borderType=BORDER_DEFAULT ) Python: cv2.filter2D(src, ddepth, kernel[, dst[,

Python pandas 0.19.1 Indexing and Selecting Data文档翻译

最近在写个性化推荐的论文,经常用到Python来处理数据,被pandas和numpy中的数据选取和索引问题绕的比较迷糊,索性把这篇官方文档翻译出来,方便自查和学习,翻译过程中难免很多不到位的地方,但大致能看懂,错误之处欢迎指正~ Python pandas 0.19.1 Indexing and Selecting Data 原文链接 http://pandas.pydata.org/pandas-docs/stable/indexing.html 数据索引和选取 pandas对象中的轴标签信息

[Python]命令行进度条

关键点是输出'\r'这个字符可以使光标回到一行的开头,这时输出其它内容就会将原内容覆盖. import time import sys def progress_test(): bar_length=20 for percent in xrange(0, 100): hashes = '#' * int(percent/100.0 * bar_length) spaces = ' ' * (bar_length - len(hashes)) sys.stdout.write("\rPercent

#Python绘制 文本进度条,带刷新、时间暂缓的

#Python绘制 文本进度条,带刷新.时间暂缓的 #文本进度条 import time as T st=T.perf_counter() print('-'*6,'执行开始','-'*6) maxx=11 #要大1 for i in range(maxx): s1='*'*i s2='->' s3='.'*(maxx-i-1) T.sleep(0.5) #假装有延时 dur=T.perf_counter()-st print("\r%3d%%[%s%s%s] %.2fs"%(i

Python.python学习(1).学习规划

Python.python学习.学习规划 欢迎收看! 阅读此文表明你也是要学Python这门神奇的语言了.很好,来对地方了,先容我简单介绍一下这个博客系列. 这个系列的博客将会持续专注于Python这个语言的知识积累和开发经验. 编写这个系列,一方面是为了巩固我自己对Python的理解,另一方面也是希望能够分享我的经验,给初学者提供一定帮助.网上现有的各类教程已经汗牛充栋,在我学习的时候就曾参阅过许多教程与文章,它们讲解问题的思路各不相同,综合的阅读使得我最终能够整理起知识的碎片并正确地理解.所

[python] python单元测试经验总结

python写单元大多数都会用到unittest和mock,测试代码覆盖率都会用到coverage,最后再用nose把所有的东西都串起来,这样每次出版本,都能把整个项目的单元测试都运行一遍. Unittest unittest就不详细介绍了,注意几点: 测试类继承unittest.TestCase 测试类.测试方法名字最好以test开头,很多工具能根据名字来自动运行,很方便 测试类里面的setUp/tearDown会在每个case执行之前/之后执行,setUpClass/tearDownClas

[python] python 中的" "和' '都是完全转义

dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"} for k in dict: print ("dict[$k] =",dict[k]) dict[$k] = grapedict[$k] = bananadict[$k] = appledi

[Python] python vs cplusplus

一些学习过程中的总结的两种语言的小对比,帮助理解OO programming. Continue... 字典 序列 --> 字典 Python: def get_counts(sequence): counts = {} for x in sequence: if x in counts: counts[x] += 1 else: counts[x] = 1 # 这是是硬伤,不优于c++,这里必须如此写 return counts c++:貌似没有这个问题. #include <iostrea

C++培训 C++初学必看的19条忠告

学习是永无止境的一个课程,想要学好,那就得看自己是怎么样的一个心态去看待学习二字!从事互联网行业更是一样的,你不跟上,那么,你就将会被淘汰! 学习任何一门语言都是一样的,高级语言尤其如此.不能说哪门语言最好,也没有人说现在学习哪门高级语言最好找工作.不管哪门语言,学好了.学熟练.学通了比什么都好.当然学通一门高级语言不是一件容易的事,但是只要不断的学习,认真听听C++的前辈们的一些忠告对C++的学习会有很大的帮助. 不要听人家说VC.BCB.BC.MC.TC等就感到很害怕,这些都只是一些集成的开