Python之Python之禅_写好Python的首要条件(Python第四篇)

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 之禅

优美好于丑陋
明了好于隐晦
简洁好于复杂
复杂好于凌乱
扁平好于嵌套
稀疏好于密集
代码可读性很重要
虽然实用性很重要,但任何特殊情况都不足以特殊到违背上述规则
不要忽视任何错误,除非有意为之
面对模棱两可的情况,拒绝享受让别人去猜测的乐趣
提供有且仅有的一种最明显解决方法
虽然起初这很难做到,除非你是荷兰人
动手行动好于什么都不做,但不加思考就行动还不如不做
如果某问题用几行代码实很难实现,那说明思路不对
如果某问题代码实现很简单,那这个实现可能走在正确的路上
命名空间是一个很棒的注意,应当多加利用

中英文

The Zen of Python, by Tim Peters
Python 之禅 作者:提姆·皮特

Beautiful is better than ugly.
优美好于丑陋(Python 以优美的代码为其风格,不要写丑陋的代码)

Explicit is better than implicit.
明了好于隐晦(Python的每一行代码、每一个变量、每一函数的意义应该是明确的,让人看了一目了然)

Simple is better than complex.
简洁好于复杂(Python的每一行代码应当是简洁的,一行代码只为一个子功能)

Complex is better than complicated.
复杂好于凌乱(如果无法避免引入复杂的代码,也要尽量保证代码之间明确简洁的关系)

Flat is better than nested.
扁平好于嵌套(Python的代码结构应该是扁平的,不应该有太多嵌套的结构)

Sparse is better than dense.
稀疏好于密集(Python代码之间应该是有间隔的,每个功能块、每个函数、每个参数、每个类之间应当有适当的间距)

Readability counts.
代码可读性很重要(Python代码具有很好的可读性,注释、变量名、函数名、类名、模块名、代码块功能等的作用和意义都是一目了然的)

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.
面对模棱两可的情况,拒绝享受让别人去猜测的乐趣
提供有且仅有的一种最明显解决方法
(解决一个问题的方法可能会有很多种,但在Python中,只选择最明显的那一个)

Although that way may not be obvious at first unless you’re Dutch.
虽然起初这很难做到,除非你是荷兰人(荷兰人暗指 Python之父:Guido van Rossum(龟叔) )

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!
命名空间是一个很棒的注意,应当多加利用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值