设为首页 加入收藏

TOP

Python的设计哲学探究(一)
2017-09-30 17:18:12 】 浏览:3423
Tags:Python 设计 哲学 探究

在Python shell中输入import this就会在屏幕上打印出来Python的设计哲学,如下:

In [25]: 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为什么是这样的,同时,我们写的代码应该是什么样的了。

今天突然好奇是怎么实现的,于是就探究了一下,先看this是啥:

In [3]: this
Out[3]: <module 'this' from '/usr/lib/python2.7/this.py'>

In [4]: type(this)
Out[4]: module

In [5]: from this import 
c  d  i  s  

可以看出来是一个模块,里边有这几个变量,同时我们可以根据路径去看this.py的内容,如下:

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

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])

打开第一反应是一脸懵逼,还以为整的其他语言呢,一看下面有一小段代码,实际上是简单的加密算法,仅仅做了简单的位移,类似于维吉尼亚加密~然后可以在终端试一下dir(this),如下:

In [5]: from this import 
c  d  i  s  

In [5]: from this import d

In [6]: d
Out[6]: 
{'A': 'N',
 'B': 'O',
 'C': 'P',
 'D': 'Q',
 'E': 'R',
 'F': 'S',

 

然后为什么引入其他模块没有不会直接执行print呢?我猜是底层做了区分…这个需要看Python源码,待我回头探究一下回来补充(真的会回来么 

 

PS:

汉语版翻译见(https://yijingping.github.io/2014/03/02/The-Zen-of-Python.html),此处摘抄如下:

The Zen of Python, by Tim Peters
Python 的设计哲学,作者: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.
面对模棱两可,拒绝猜测。
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python中的绝对路劲和相对路径 下一篇Python 基础-python环境变量、模..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目