像pythonista一样码代码

Code Like a Pythonista: Idiomatic Python

最近在看python代码规范的英文版!发现了一些有意思的地方,翻译成中文后很好玩,所以零碎的记录下。

原文地址:http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#id6

一、

You can decide for yourself if you're a "Pythoneer" or a "Pythonista". The terms have somewhat different connotations.

如果你是一个Pythonner或者是Pythonista,你可以为自己做出决定。这些不一样规则(指代码规范),都多少包含了一些不一样的意义!

二、

When in doubt:

import this

Try it in a Python interactive interpreter:

>>> import this

Here's another easter egg:

>>> from __future__ import braces
  File "<stdin>", line 1
SyntaxError: not a chance

What a bunch of comedians! 😃

当你对一些语法有些不确定的时候,打开你的编译器去执行他,你会发现一些语法错误的彩蛋,这真他妈的有趣(Excuse me?)

三、

Programs must be written for people to read, and only incidentally for machines to execute.Try to make your programs easy to read and obvious.

程序写出来是给人看的,交给机器执行只不过是顺带的行为。(哇哦!酷)尽可能的让你的程序易于阅读和理解。

四、

Backslashes are fragile; they must end the line they're on. If you add a space after the backslash, it won't work any more. Also, they're ugly.

反斜线这个设计实在太蠢了,他们必须要在一行的末尾才出现,如果我们在反斜线后面加一个空格,那整个程序都完了!所以这个设计真是蠢爆了!

五、

But try to avoid the __private form. I never use it. Trust me. If you use it, you WILL regret it later.

千万要避免使用双下划线的形式定义私有变量,我从来没这么用过。相信我,如果你这么用了你迟早会后悔!(喂,没用过你就叫我别用?)

六、

Adjacent literal strings are concatenated by the parser:

>>> print 'o' 'n' "e"
one

The spaces between literals are not required, but help with readability. Any type of quoting can be used:

>>> print 't' r'\/\/' """o"""
t\/\/o

The string prefixed with an "r" is a "raw" string. Backslashes are not evaluated as escapes in raw strings. They're useful for regular expressions and Windows filesystem paths.

Note named string objects are not concatenated:

>>> a = 'three'
>>> b = 'four'
>>> a b
  File "<stdin>", line 1
    a b
      ^
SyntaxError: invalid syntax

That's because this automatic concatenation is a feature of the Python parser/compiler, not the interpreter. You must use the "+" operator to concatenate strings at run time.

文字太多,简单来讲就是python中字符串的自动拼接功能是由python编译器去完成的而底层的翻译器并不会给你做自动拼接字符串的工作,所以当我们用print a b 的时候两个字符串会自动拼接,而直接使用 a b 则会报语法错误。

七、

Docstrings = How to use code

Comments = Why (rationale) & how code works

Docstrings explain how to use code, and are for the users of your code. Uses of docstrings:

  • Explain the purpose of the function even if it seems obvious to you, because it might not be obvious to someone else later on.
  • Describe the parameters expected, the return values, and any exceptions raised.
  • If the method is tightly coupled with a single caller, make some mention of the caller (though be careful as the caller might change later).

Comments explain why, and are for the maintainers of your code. Examples include notes to yourself, like:

# !!! BUG: ...

# !!! FIX: This is a hack

# ??? Why is this here?

Both of these groups include you, so write good docstrings and comments!

Docstrings are useful in interactive use (help()) and for auto-documentation systems.

False comments & docstrings are worse than none at all. So keep them up to date! When you make changes, make sure the comments & docstrings are consistent with the code, and don't contradict it.

文档解释了如何使用这段代码,注释解释了代码是如何工作的

文档是给用户看的,注释是给自己和维护这段代码的人看的

文档解释了代码执行的目标尽管这对你来说已经明确无比,因为对于其他人来说你的代码可能难以快速理解。文档描述了所期望的参数,函数所返回的值以及各种意外情况。如果某个方法和某个调用者是紧耦合的,那我们必须对这个调用方法进行一些介绍。

文档和注释对我们自己也很有用,所以用心的编写好文档和注释吧

文档在交互使用和自动文档系统是非常有用的(自动文档系统。。)

错误的文档和注释还不如没有他们,所以用心的维护它们,让你的程序能符合它们的描述吧。

(好累,而且没什么亮点!但是很重要,想了想翻出来了)

八、

A foolish consistency is the hobgoblin of little minds.

愚蠢的稳定性对弱小的人来说就像个妖怪。

大概是说程序的架构很蠢,但是在很蠢的架构下已经累积起了大量的代码!这个时候到底改不改架构呢?有勇气的人自然大刀阔斧的改革,弱小的人则会继续在愚蠢的架构下苟活,不敢也无法反抗!

九、
from xxx import *

Wild-card imports are from the dark side of Python.

Never!

The from module import * wild-card style leads to namespace pollution. You'll get things in your local namespace that you didn't expect to get. You may see imported names obscuring module-defined local names. You won't be able to figure out where certain names come from. Although a convenient shortcut, this should not be in production code.

Moral: don't use wild-card imports!

万能导入属于Python的黑暗面。

就像黑暗料理一样!你不会想吃第二遍!

使用from module import *这种万能导入方式,会导致整个命名空间被污染。使用这个方式,你将会在尝试获取本地命名空间的方法的时候拿到你不想要的东西。你会看到被导入的这些名字会让模块定义的这些本地名字变得模糊。你将无法指出那些名字确切的来源。即使它看起来方便很多,但是我们决不能让这个导入方式出现在生产代码中。

再重申一遍:谁用万能导入谁是SX!

十、

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. In other words, keep your programs simple!

最开始的时候,调试一段代码的难度是你写出这段代码的难度的两倍!所以你在写代码的时候表现的越聪明代码越巧妙,很明显的,你在调试代码的时候就会显得越愚蠢!换句话说,请保持你的代码简洁!

 

翻译英文还是挺好玩的!尤其是里面有自己的想法的时候!而且在看之前我觉得挺多的这篇文档,但是当我尝试去翻译的时候,发现很快就翻到底部了!

 

本文由本人原创,创作不易,转载请注明出处!

posted on 2016-05-26 16:22  捞月丶  阅读(1262)  评论(0编辑  收藏  举报

导航