编程规约

来自谷歌规范。https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules/#comments

========注释=========

1. 文档字符串是包, 模块, 类或函数里的第一个语句。这些字符串可以通过对象的__doc__成员被自动提取, 并且被pydoc所用。我们对文档字符串的惯例是使用三重双引号”“”( PEP-257 )。

首先是一行以句号, 问号或惊叹号结尾的概述(或者该文档字符串单纯只有一行). 
接着是一个空行. 接着是文档字符串剩下的部分, 它应该与文档字符串的第一行的第一个引号对齐.

2. 模块:每个文件应该包含一个许可样板. 根据项目使用的许可(例如, Apache 2.0, BSD, LGPL, GPL), 选择合适的样板。

3. 函数和方法

  下文所指的函数,包括函数, 方法, 以及生成器.

  一个函数必须要有文档字符串, 除非它满足以下条件:

    外部不可见
    非常短小
    简单明了
  文档字符串应该包含函数做什么, 以及输入和输出的详细描述. 通常, 不应该描述”怎么做”, 除非是一些复杂的算法. 文档字符串应该提供足够的信息, 当别人编写代码调用该函数时, 他不需要看一行代码, 只要看文档字符串就可以了. 对于复杂的代码, 在代码旁边注释会比使用文档字符串更有意义.

  关于函数的几个方面应该在特定的小节中进行描述记录, 这几个方面如下文所述. 每节应该以一个标题行开始. 标题行以冒号结尾. 除标题行外, 节的其他内容应被缩进2个空格.

  Args:
    列出每个参数的名字, 并在名字后使用一个冒号和一个空格, 分隔对该参数的描述.如果描述太长超过了单行80字符,使用2或者4个空格的悬挂缩进(与文件其他部分保持一致). 描述应该包括所需的类型和含义. 如果一个函数接受*foo(可变长度参数列表)或者**bar (任意关键字参数), 应该详细列出*foo和**bar.
  Returns: (或者 Yields: 用于生成器)
    描述返回值的类型和语义. 如果函数返回None, 这一部分可以省略.
  Raises:
    列出与接口有关的所有异常.

 1 def fetch_bigtable_rows(big_table, keys, other_silly_variable=None):
 2     """Fetches rows from a Bigtable.
 3 
 4     Retrieves rows pertaining to the given keys from the Table instance
 5     represented by big_table.  Silly things may happen if
 6     other_silly_variable is not None.
 7 
 8     Args:
 9         big_table: An open Bigtable Table instance.
10         keys: A sequence of strings representing the key of each table row
11             to fetch.
12         other_silly_variable: Another optional variable, that has a much
13             longer name than the other args, and which does nothing.
14 
15     Returns:
16         A dict mapping keys to the corresponding table row data
17         fetched. Each row is represented as a tuple of strings. For
18         example:
19 
20         {'Serak': ('Rigel VII', 'Preparer'),
21          'Zim': ('Irk', 'Invader'),
22          'Lrrr': ('Omicron Persei 8', 'Emperor')}
23 
24         If a key from the keys argument is missing from the dictionary,
25         then that row was not found in the table.
26 
27     Raises:
28         IOError: An error occurred accessing the bigtable.Table object.
29     """
30     pass

待续。。。

========

原文地址:https://www.cnblogs.com/chenhongarticles/p/10060667.html