Google Python 编码规范指南

论坛 期权论坛 期权     
Python那些事   2019-6-16 04:40   3347   0
(点击上方快速关注并设置为星标,一起学Python)
来源:KotlinPython   链接:
https://mp.weixin.qq.com/s/-aM6AJ2oUnMYjmanbLH0LA
我是PythonGao。 一名微软工程师。今天给大家分享一下Google Python 编程规范。适合入门者学习。

分号

不要在行尾加分号, 也不要用分号将两条命令放在同一行.

行长度

每行不超过80个字符
例外:
  • 长的导入模块语句
  • 注释里的URL


不要使用反斜杠连接行.
Python会将 圆括号, 中括号和花括号中的行隐式的连接起来 , 你可以利用这个特点. 如果需要, 你可以在表达式外围增加一对额外的圆括号
  1. Yes: foo_bar(self, width, height, color='black', design=None, x='foo',
  2.              emphasis=None, highlight=0)
  3.      if (width == 0 and height == 0 and
复制代码
  1.          color == 'red' and emphasis == 'strong'):
复制代码


如果一个文本字符串在一行放不下, 可以使用圆括号来实现隐式行连接:
  1. x = ('This will build a very long long '
  2.      'long long long long long long string')
复制代码
在注释中,如果必要,将长的URL放在一行上。
  1. Yes:  # See details at      # http://www.example.com/us/developer/documentation/api/content/v2.0/csv_file_name_extension_full_specification.htmlNo:  # See details at     # http://www.example.com/us/developer/documentation/api/content/     # v2.0/csv_file_name_extension_full_specification.html
复制代码
注意上面例子中的元素缩进; 你可以在本文的 缩进 部分找到解释.
括号
宁缺毋滥的使用括号
除非是用于实现行连接, 否则不要在返回语句或条件语句中使用括号. 不过在元组两边使用括号是可以的
  1. Yes: if foo:
  2.          bar()
  3.      while x:
  4.          x = bar()
  5.      if x and y:
  6.          bar()
  7.      if not x:
  8.          bar()
  9.      return foo
  10.      for (x, y) in dict.items(): ...
  11. No:  if (x):
  12.          bar()
  13.      if not(x):
  14.          bar()
  15.      return (foo)
复制代码

缩进

用4个空格来缩进代码
绝对不要用tab, 也不要tab和空格混用. 对于行连接的情况, 你应该要么垂直对齐换行的元素(见 行长度 部分的示例), 或者使用4空格的悬挂式缩进(这时第一行不应该有参数):
  1. Yes:   # Aligned with opening delimiter       foo = long_function_name(var_one, var_two,                                var_three, var_four)       # Aligned with opening delimiter in a dictionary       foo = {           long_dictionary_key: value1 +                                value2,           ...       }       # 4-space hanging indent; nothing on first line       foo = long_function_name(           var_one, var_two, var_three,           var_four)       # 4-space hanging indent in a dictionary       foo = {           long_dictionary_key:               long_dictionary_value,           ...       }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)      # No hanging indent in a dictionary      foo = {          long_dictionary_key:              long_dictionary_value,              ...      }
复制代码
空行

顶级定义之间空两行, 方法定义之间空一行
顶级定义之间空两行, 比如函数或者类定义. 方法定义, 类定义与第一个方法之间, 都应该空一行. 函数或方法中, 某些地方要是你觉得合适, 就空一行.
空格
按照标准的排版规范来使用标点两边的空格
括号内不要有空格.
  1. Yes: spam(ham[1], {eggs: 2}, [])No:  spam( ham[ 1 ], { eggs: 2 }, [ ] )
复制代码
不要在逗号, 分号, 冒号前面加空格, 但应该在它们后面加(除了在行尾).
  1. Yes: if x == 4:         print x, y     x, y = y, xNo:  if x == 4 :         print x , y     x , y = y , x
复制代码
参数列表, 索引或切片的左括号前不应加空格.
  1. 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). 至于算术操作符两边的空格该如何使用, 需要你自己好好判断. 不过两侧务必要保持一致.
[code]Yes: x == 1No:  x
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:100
帖子:20
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP