Exploration of Python design philosophy and python Philosophy

Source: Internet
Author: User

Exploration of Python design philosophy and python Philosophy

Input import this in Python shell to print the Python design philosophy on the screen, as shown below:

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!                                                                                                                                                                                                                                                                                                                                 

Let's take a look at it and understand why Python is like this. At the same time, what kind of code should we write.

Today, I suddenly wondered how it was implemented, so I explored it. Let's first look at what this is:

In [3]: thisOut[3]: <module 'this' from '/usr/lib/python2.7/this.py'>In [4]: type(this)Out[4]: moduleIn [5]: from this import c  d  i  s  

It can be seen that it is a module with these variables. At the same time, we can view the content of this. py according to the path, as shown below:

s = """Gur Mra bs Clguba, ol Gvz CrgrefOrnhgvshy 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])

The first reaction was to look at the other language. I thought it was another whole language. I thought there was a small piece of code below. It was actually a simple encryption algorithm, which only made a simple shift, similar to Virginia encryption ~ Then try dir (this) on the terminal, as shown below:

In [5]: from this import c  d  i  s  In [5]: from this import dIn [6]: dOut[6]: {'A': 'N', 'B': 'O', 'C': 'P', 'D': 'Q', 'E': 'R', 'F': 'S',

 

Then why didn't other modules directly execute print? I guess the bottom layer makes a distinction... This requires reading the Python source code. I will explore it later to add it (will it actually come back?

 

PS:

See Chinese translation (https://yijingping.github.io/2014/03/02/The-Zen-of-Python.html), here excerpt is as follows:

The Zen of Python, by Tim PetersPython's design philosophy by Tim PetersBeautiful is better than uugly. Elegance is better than ugliness. Explicit is better than implicit. Clear is better than vague. Simple is better than complex. Simplicity is better than complexity. Complex is better than complicated. Flat is better than nested. Flat is better than nested. Sparse is better than dense. The interval is better than compact. Readability counts. Readability is important. Special cases aren't special enough to break the rules. These principles should not be broken even if you use a Special name. Although practicality beats purity. Errors shoshould never pass silently. Errors cannot be ignored. Unless explicitly silenced. Unless otherwise specified. In the face of ambiguity, refuse the temptation to guess. In the face of ambiguity, refuse to guess. There shoshould be one -- and preferably only one -- obvious way to do it. There is one way, preferably only one way to do one thing. Although that way may not be obvious at first unless you're Dutch. Although this method is not easy at the beginning, unless you are the father of Python. Now is better than never. But from Now on, it is always better than never. Although never is often better than * right * now. Although "not done" is often better than "Done now. If the implementation is hard to explain, it's a bad idea. If an implementation is not easy to explain, it must be a bad idea. If the implementation is easy to explain, it may be a good idea. If an implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! Just like namespace is a wonderful idea and should be used more.

 

 

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.