注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

与牛熊共舞

2010与牛熊共舞,共同创造一个神话

 
 
 

日志

 
 

Python 之import this  

2011-01-29 21:18:10|  分类: Python |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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

  进而搜索了下,发现有意思的还真有不少:

 
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!
名称空间是一个好东西——让我们做更多那样的东西!

Before writing any code,
? ? ? ?
Check Python's standard library.

Check the Python Package Index (the "Cheese Shop"):

http://cheeseshop.python.org/pypi

Search the web. Google is your friend.

 

以动手实践为荣 , 以只看不练为耻; 
以打印日志为荣 , 以单步跟踪为耻; 
以空格缩进为荣 , 以制表缩进为耻; 
以单元测试为荣 , 以人工测试为耻; 

以模块复用为荣 , 以复制粘贴为耻; 
以多态应用为荣 , 以分支判断为耻; 
以Pythonic为荣 , 以冗余拖沓为耻; 
以总结分享为荣 , 以X求其解为耻; 

 

#coding=utf-8

s = """Gur Mra bs Clguba, ol Gvz Crgref
 
Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
Nygubhtu cenpgvpnyvgl orngf chevgl.
Reebef fubhyq arire cnff fvyragyl.
Hayrff rkcyvpvgyl fvyraprq.
Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
Abj vf orggre guna arire.
Nygubhtu arire vf bsgra orggre guna *evtug* abj.
Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!


python之禅

优美胜于丑陋(以编写优美的代码为目标)
明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似)
简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)
复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)
扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)
间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题)
可读性很重要(优美的代码是可读的)
即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上)
不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写 except:pass 风格的代码)
当存在多种可能,不要尝试去猜测,而是尽量找一种,最好是唯一一种明显的解决方案(如果不确定,就用穷举法)
虽然这并不容易,因为你不是 Python 之父(这里的 Dutch 是指 Guido )
 
做也许好过不做,但不假思索就动手还不如不做(动手之前要细思量)
 
如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然(方案测评标准)
 
命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)


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

d = {}
for c in (65, 97):
    for i in range(26):
        d[chr(i + c)] = chr((i + 13) % 26 + c)
print "".join([d.get(c, c) for c in s])

  评论这张
 
阅读(944)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017