作为文档的Docstring一般出现在模块头部、函数和类的头部,
这样在python中可以通过对象的__doc__对象获取文档,
编辑器和IDE也可以根据Docstring给出自动提示。
1) 所有的公共模块、函数、类、方法,都应该写 docstring 。 私有方法不一定需要,但应该在 def 后提供一个块注释来说明。
~~~
"""Return a foobar
Optional plotz says to frobnicate the bizbaz first.
"""
"""Oneline docstring"""
~~~
2) 文档注释以 """ 开头和结尾, 首行不换行, 如有多行, 末行必需换行, 以下是Google的docstring风格示例
~~~
# -*- coding: utf-8 -*-
"""Example docstrings.
This module demonstrates documentation as specified by the `Google Python
Style Guide`_. Docstrings may extend over multiple lines. Sections are created
with a section header and a colon followed by a block of indented text.
Example:
Examples can be given using either the ``Example`` or ``Examples``
sections. Sections support any reStructuredText formatting, including
literal blocks::
$ python example_google.py
Section breaks are created by resuming unindented text. Section breaks
are also implicitly created anytime a new section starts.
"""
~~~
4) 不要在文档注释复制函数定义原型, 而是具体描述其具体内容, 解释具体参数和返回值等
~~~
# 不推荐的写法(不要写函数原型等废话)
def function(a, b):
"""function(a, b) -> list"""
... ...
# 正确的写法
def function(a, b):
"""计算并返回a到b范围内数据的平均值"""
... ...
~~~
6) 对函数参数、返回值等的说明采用numpy标准, 如下所示
~~~
def func(arg1, arg2):
"""在这里写函数的一句话总结(如: 计算平均值).
这里是具体描述.
参数
----------
arg1 : int
arg1的具体描述
arg2 : int
arg2的具体描述
返回值
-------
int
返回值的具体描述
参看
--------
otherfunc : 其它关联函数等...
示例
--------
示例使用doctest格式, 在`>>>`后的代码可以被文档测试工具作为测试用例自动运行
>>> a=[1,2,3]
>>> print [x + 3 for x in a]
[4, 5, 6]
"""
~~~
8) 文档注释不限于中英文, 但不要中英文混用
9) 文档注释不是越长越好, 通常一两句话能把情况说清楚即可
10) 搜索c
- 前言
- Python编程规范
- 编码
- 代码
- 缩进、行宽、引号、空行
- 空格
- 换行
- import
- 注释
- 代码注释
- 文档注释(Docstring)
- 命名规范
- 数据结构
- 变量
- 变量作用域
- 命名空间
- 作用域
- python作用域
- 对象
- 序列
- 可迭代对象
- 迭代器
- 生成器
- 可迭代对象 & 迭代器 & 生成器
- 整数池 & 字符串intern
- 数据类型
- 数字
- int
- float
- NaN
- 四舍五入 & 取整
- 列表
- 元组
- 字典
- 集合
- 字符串
- 字符集&字符编码
- 字符串&字节串
- 字符串函数
- 字符串格式化
- str.format
- Formatted string literals
- format函数
- string.Formatter类
- %
- Format String Syntax
- Format Specification Mini-Language
- fill
- align
- sign
- #
- 0
- width
- grouping_option
- .precision
- type
- locale
- Python3 locale 模块
- 语句
- 运算符
- if/else
- for...in
- while
- break/continue
- 函数
- 函数
- 函数参数
- 递归函数
- 匿名函数
- 高阶函数
- map
- reduce
- filter
- sorted
- 返回函数
- 闭包
- 装饰器
- 函数装饰器
- 带参数的装饰器
- 类装饰器
- 带参数的类装饰器
- 偏函数
- 面向对象
- 类 & 实例
- 属性
- 方法
- 访问限制
- 继承
- 新式类 & 经典类
- MRO
- MixIn
- 模块
- 特殊变量
- 编写模块
- 引入 & 重载
- 搜索模块
- 第三方模块
- 常见模块
- 标准库
- os
- sys
- datetime
- re
- urllib
- time/datetime
- threading
- multiprocessing
- builtins
- help
- range
- enumerate
- 异同
- str() repr() ascii()
- exit()、sys.exit()、os._exit()
- 数据库
- mysql
- 错误、调试、测试
- 异常
- 异常处理
- 自定义异常
- 抛出异常
- 调试
- logging
- pdb
- 线程&&进程
- 线程
- 杂
- python 脚本传参
- python无关
- redis
- mongo
- linux
- mysql简略