楼主: 
tiefblau (tiefblau)   
2013-05-13 16:58:48※ [本文转录自 Python 看板 #1HaAcu03 ]
作者: tiefblau (tiefblau) 看板: Python
标题: [翻译] Google 建议的 Python 风格指南 20~26
时间: Mon May 13 16:52:05 2013
原文网址:http://google-styleguide.googlecode.com/svn/trunk/pyguide.html
Python Style Rules
*Semicolons 
不要使用分号作为每行结尾
也不要使用分号将两个指令塞进同一行
*Line length
每行最大长度为80字符
例外:
    1.较长的 import 语句
    2.注解中的URL
不要用反斜杠("\")来继续句子
使用Python隐含的连接句子方式:"()","[]","{}"。
如果需要的话,可加入额外的括号。
Yes:
foo_bar(self, width, height, color='black', design=None, x='foo',
        emphasis=None, highlight=0)
     if (width == 0 and height == 0 and
         color == 'red' and emphasis == 'strong'):
如果一个字串无法被写为一行,使用"()"来连接
x = ('This will build a very long long '
     'long long long long long long string')
注解时,如遇需求,将长的URL放在同一行。
Yes:
     # See details at
     # http://www.example.com/us/developer/documentation/api/content/v2.0/csv_file_name_extension_full_specification.html
No:
     # See details at
     # http://www.example.com/us/developer/documentation/api/content/\
     # v2.0/csv_file_name_extension_full_specification.html
注意以上有连接句子时缩排的格式;可查看本指南关于缩排的说明。
*Parentheses
节俭地使用括号
不要使用"()"包住 回传 或 条件 的语句,除非作为连接句子之用(参考上一点)。
但可用于包住tuples。
Yes:
     if foo:
         bar()
     while x:
         x = bar()
     if x and y:
         bar()
     if not x:
         bar()
     return foo
     for (x, y) in dict.items(): ...
No:
     if (x):
         bar()
     if not(x):
         bar()
     return (foo)
*Indentation
用四个空白来缩排你的程式码。
不要用 tab 或是 tab混空白键 来缩排你的程式码
遇到应该连接在一起的句子时,可选择使用
    垂直对齐,如同*line length(此指南第21点)的例子一般
    四格空白的hanging indent(参考下例),用于第一行没有引数的情况时。
Yes:
       # Aligned with opening delimiter
       foo = long_function_name(var_one, var_two,
                                var_three, var_four)
       # 4-space hanging indent; nothing on first line
       foo = long_function_name(
           var_one, var_two, var_three,
           var_four)
No:
       # Stuff on first line forbidden
       foo = long_function_name(var_one, var_two,
           var_three, var_four)
       # 2-space hanging indent forbidden
       foo = long_function_name(
         var_one, var_two, var_three,
         var_four)
*Blank Lines
top-level definitions之间:空两行
method    definitions之间:空一行
在函式或是类别的定义之间空两行
方法(method)之间 、 class宣告行与第一个方法之间,都是空一行
Use single blank lines as you judge appropriate within functions or methods.
#看不太懂这句,求解。
*Whitespace
遵守一般的方式在标点符号附近使用空白。
在"()","[]","{}"内不用空白
    Yes:
        spam(ham[1], {eggs: 2}, [])
    No:
        spam( ham[ 1 ], { eggs: 2 }, [ ] )
在"," , ";" , ":"加入一个空白,前面不需要。
Yes:
    if x == 4:
        print x, y
    x, y = y, x
No:
    if x == 4 :
        print x , y
    x , y = y , x
参数列、索引值、切片时用的"()","[]"前不应有空白。
Yes: spam(1)
No:  spam (1)
Yes: dict['key'] = list[index]
No:  dict ['key'] = list [index]
在二元运算的两端都加上一个空白:赋值("="),比较("==","<",">","!=","<>","<=",
">=","in","not in","is","is not"),布林运算子("and","or","not")
用你的判断在算术符号间加入空白,但是永远记得在二元运算的两端都要同时加上空白。
Yes: x == 1
No:  x<1
表达keyword argument或default parameter value时所用的"="周围不要使用空白
Yes: def complex(real, imag=0.0): return magic(r=real, i=imag)
No:  def complex(real, imag = 0.0): return magic(r = real, i = imag)
不要使用空白达到连续几行的垂直对齐效果,这会造成维护上的困难。
(适用于:":","#"...etc)
Yes:
  foo = 1000  # comment
  long_name = 2  # comment that should not be aligned
  dictionary = {
      'foo': 1,
      'long_name': 2,
      }
No:
  foo       = 1000  # comment
  long_name = 2     # comment that should not be aligned
  dictionary = {
      'foo'      : 1,
      'long_name': 2,
      }
*Shebang Line
大部分的 .py 档不须以 #! 为开头。
以 #!/usr/bin/python 作为 main file 的开头。
本行是用来让kernel找到Python的直译器,但在导入模组(importing modules)时会被忽略
因此,只有会被直接执行的档案需要这行。
===
今天的都短短的,不知不觉就翻了好多点
下一个好长阿~~~   容许我偷懒一下 Orz