Python中的多行注释文档编写风格汇总


Posted in Python onJune 16, 2016

什么是docstring

在软件工程中,其实编码所占的部分是非常小的,大多是其它的事情,比如写文档。文档是沟通的工具。
在Python中,比较推崇在代码中写文档,代码即文档,比较方便,容易维护,直观,一致。
代码写完,文档也出来了。其实Markdown也差不多这种思想,文本写完,排版也完成了。
看看PEP 0257中对docstring的定义:

A docstring is a string literal that occurs as the first statement in
a module, function, class, or method definition. Such a docstring
becomes the __doc__ special attribute of that object.
简单来说,就是出现在模块、函数、类、方法里第一个语句的,就是docstring。会自动变成属性__doc__。

def foo():
  """ This is function foo"""

可通过foo.__doc__访问得到' This is function foo'.

各类docstring风格:

Epytext

这是曾经比较流行的一直类似于javadoc的风格。

"""
This is a javadoc style.

@param param1: this is a first param
@param param2: this is a second param
@return: this is a description of what is returned
@raise keyError: raises an exception
"""

reST

这是现在流行的一种风格,reST风格,Sphinx的御用格式。我个人也是喜欢用这种风格,比较紧凑。

"""
This is a reST style.

:param param1: this is a first param
:param param2: this is a second param
:returns: this is a description of what is returned
:raises keyError: raises an exception
"""

Google风格

"""
This is a groups style docs.

Parameters:
  param1 - this is the first param
  param2 - this is a second param

Returns:
  This is a description of what is returned

Raises:
  KeyError - raises an exception
"""

Numpydoc (Numpy风格)

"""
My numpydoc description of a kind
of very exhautive numpydoc format docstring.

Parameters
----------
first : array_like
  the 1st param name `first`
second :
  the 2nd param
third : {'value', 'other'}, optional
  the 3rd param, by default 'value'

Returns
-------
string
  a value in a string

Raises
------
KeyError
  when a key error
OtherError
  when an other error
"""

docstring工具之第三方库pyment

用来创建和转换docstring.
使用方法就是用pyment生成一个patch,然后打patch。

$ pyment test.py      #生成patch
$ patch -p1 < test.py.patch #打patch

详情:https://github.com/dadadel/pyment

使用sphinx的autodoc自动从docstring生产api文档,不用再手写一遍

我在代码中已经写过docstring了,写api文档的内容跟这个差不多,难道要一个一个拷贝过去rst吗?当然不用。sphinx有autodoc功能。
首先编辑conf.py文件,
1. 要有'sphinx.ext.autodoc'这个extensions
2. 确保需要自动生成文档的模块可被import,即在路径中。比如可能需要sys.path.insert(0, os.path.abspath(‘../..'))

然后,编写rst文件,

xxx_api module
---------------------

.. automodule:: xxx_api
  :members:
  :undoc-members:
  :show-inheritance:
敲make html命令,就可以从docstring中生成相关的文档了,不用多手写一遍rst.
看效果:
Python中的多行注释文档编写风格汇总
Python 相关文章推荐
Python中的MongoDB基本操作:连接、查询实例
Feb 13 Python
在Python中用has_key()方法查找键是否存在的教程
May 21 Python
Python写入CSV文件的方法
Jul 08 Python
在类Unix系统上开始Python3编程入门
Aug 20 Python
django rest framework之请求与响应(详解)
Nov 06 Python
numpy matrix和array的乘和加实例
Jun 28 Python
Python中文编码知识点
Feb 18 Python
详解python列表(list)的使用技巧及高级操作
Aug 15 Python
Python利用matplotlib绘制约数个数统计图示例
Nov 26 Python
Python基于Tensor FLow的图像处理操作详解
Jan 15 Python
Python数组并集交集补集代码实例
Feb 18 Python
Python使用Matlab命令过程解析
Jun 04 Python
Python构造自定义方法来美化字典结构输出的示例
Jun 16 #Python
浅谈Python中chr、unichr、ord字符函数之间的对比
Jun 16 #Python
详解Python中 __get__和__getattr__和__getattribute__的区别
Jun 16 #Python
Python利用带权重随机数解决抽奖和游戏爆装备问题
Jun 16 #Python
Python黑魔法@property装饰器的使用技巧解析
Jun 16 #Python
Python实现类似jQuery使用中的链式调用的示例
Jun 16 #Python
浅析Python中else语句块的使用技巧
Jun 16 #Python
You might like
德生H-501的评价与改造
2021/03/02 无线电
PHP中防止SQL注入攻击和XSS攻击的两个简单方法
2010/04/15 PHP
php获取指定(访客)IP所有信息(地址、邮政编码、国家、经纬度等)的方法
2015/07/06 PHP
详解php的socket通信
2015/08/11 PHP
windows下apache搭建php开发环境
2015/08/27 PHP
学习ExtJS accordion布局
2009/10/08 Javascript
下拉菜单点击实现连接跳转功能的js代码
2013/05/19 Javascript
window.showModalDialog()返回值的学习心得总结
2014/01/07 Javascript
JavaScript通过正则表达式实现表单验证电话号码
2014/03/07 Javascript
node.js中的console.error方法使用说明
2014/12/10 Javascript
Yii2使用Bootbox插件实现自定义弹窗
2015/04/02 Javascript
无阻塞加载js,防止因js加载不了影响页面显示的问题
2016/12/18 Javascript
AngularJS执行流程详解
2017/02/17 Javascript
bootstrap datetimepicker 日期插件在火狐下出现一条报错信息的原因分析及解决办法
2017/03/08 Javascript
React-Native中props具体使用详解
2017/09/04 Javascript
Angular实现的日程表功能【可添加及隐藏显示内容】
2017/12/27 Javascript
react以create-react-app为基础创建项目
2018/03/14 Javascript
用ES6写全屏滚动插件的示例代码
2018/05/02 Javascript
Angular5中提取公共组件之radio list的实例代码
2018/07/10 Javascript
迅速了解一下ES10中Object.fromEntries的用法使用
2019/03/05 Javascript
Layui弹框中数据表格中可双击选择一条数据的实现
2020/05/06 Javascript
聊聊vue 中的v-on参数问题
2021/01/29 Vue.js
python中enumerate函数用法实例分析
2015/05/20 Python
python多进程中的内存复制(实例讲解)
2018/01/05 Python
python合并同类型excel表格的方法
2018/04/01 Python
在Python中输入一个以空格为间隔的数组方法
2018/11/13 Python
Python 3.x基于Xml数据的Http请求方法
2018/12/28 Python
Python根据成绩分析系统浅析
2019/02/11 Python
python数据化运营的重要意义
2019/11/25 Python
python和php哪个容易学
2020/06/19 Python
python 使用cycle构造无限循环迭代器
2020/12/02 Python
Web页面中八种创建多列等高(等高列布局)的实现技术
2012/12/24 HTML / CSS
使用CSS3编写类似iOS中的复选框及带开关的按钮
2016/04/11 HTML / CSS
来自南加州灵感的工作和娱乐服装:TravisMathew
2019/05/01 全球购物
公安局班子个人对照检查材料思想汇报
2014/10/09 职场文书
Oracle 触发器trigger使用案例
2022/02/24 Oracle