通常用过Python的人,基本上都晓得在交互式诠释器中输入import this就会显现Tim Peters 的The Zen of Python,但它那偈语般的语句有点令人费解,所以我想分享一下我对它的体味,顺带给出我的翻译。
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之禅 by Tim Peters
幽美胜于貌寝(Python 以编写幽美的代码为目的)
清楚明了胜于艰涩(幽美的代码应该是清楚明了的,定名范例,作风类似)
简约胜于庞杂(幽美的代码应该是简约的,不要有庞杂的内部完成)
庞杂胜于缭乱(假如庞杂不可避免,那代码间也不能有难明的关联,要坚持接口简约)
扁平胜于嵌套(幽美的代码应该是扁平的,不能有太多的嵌套)
距离胜于紧凑(幽美的代码有恰当的距离,不要期望一行代码解决问题)
可读性很主要(幽美的代码是可读的)
即使假借惯例的实用性之名,也不可违犯这些划定规矩(这些划定规矩高高在上)
不要包涵一切毛病,除非你一定须要如许做(精准地捕捉非常,不写 except:pass 作风的代码)
当存在多种能够,不要尝试去猜想
而是只管找一种,最好是唯一一种显著的解决计划(假如不一定,就用穷举法)
虽然这并不轻易,由于你不是 Python 之父(这里的 Dutch 是指 Guido )
做或许好过不做,但不假思索就着手还不如不做(着手之前要细思考)
假如你没法向人形貌你的计划,那一定不是一个好计划;反之亦然(计划测评规范)
定名空间是一种绝妙的理念,我们应该多加应用(提倡与召唤)
以上就是python之禅是什么意义的细致内容,更多请关注ki4网别的相干文章!