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中urllib+urllib2+cookielib模块编写爬虫实战
Jan 20 Python
Mac 上切换Python多版本
Jun 17 Python
Python2和Python3之间的str处理方式导致乱码的讲解
Jan 03 Python
django与小程序实现登录验证功能的示例代码
Feb 19 Python
Python对象转换为json的方法步骤
Apr 25 Python
Python生成MD5值的两种方法实例分析
Apr 26 Python
Django实现auth模块下的登录注册与注销功能
Oct 10 Python
关于tf.nn.dynamic_rnn返回值详解
Jan 20 Python
Python过滤序列元素的方法
Jul 31 Python
python Matplotlib数据可视化(1):简单入门
Sep 30 Python
深入理解Python变量的数据类型和存储
Feb 01 Python
详解Java中一维、二维数组在内存中的结构
Feb 11 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
批量获取memcache值并按key的顺序返回的实现代码
2011/06/14 PHP
php操作mysql数据库的基本类代码
2014/02/25 PHP
getJSON跨域SyntaxError问题分析
2014/08/07 PHP
php使用cookie保存用户登录的用户名实例
2015/01/26 PHP
PHP实现二叉树的深度优先与广度优先遍历方法
2015/09/28 PHP
html 锁定页面(js遮罩层弹出div效果)
2009/10/27 Javascript
jquery 选择器部分整理
2009/10/28 Javascript
JQuery扩展插件Validate 1 基本使用方法并打包下载
2011/09/05 Javascript
Jquery submit()无法提交问题
2013/04/21 Javascript
JQuery中如何传递参数如click(),change()等具体实现
2013/04/28 Javascript
jQuery插件的写法分享
2013/06/12 Javascript
自动设置iframe大小的jQuery代码
2013/09/11 Javascript
jquery Ajax 实现加载数据前动画效果的示例代码
2014/02/07 Javascript
JavaScript中的正则表达式简明总结
2014/04/04 Javascript
Javascript 正则表达式实现为数字添加千位分隔符
2015/03/10 Javascript
JavaScript的new date等日期函数在safari中遇到的坑
2016/10/24 Javascript
用js控件div的滚动条,让它在内容更新时自动滚到底部的实现方法
2016/10/27 Javascript
Bootstrap CSS组件之下拉菜单(dropdown)
2016/12/17 Javascript
jQuery表单设置值的方法
2017/06/30 jQuery
基于Vue.js 2.0实现百度搜索框效果
2020/12/28 Javascript
js+canvas绘制图形验证码
2020/09/21 Javascript
Vue向后台传数组数据,springboot接收vue传的数组数据实例
2020/11/12 Javascript
python 字典(dict)遍历的四种方法性能测试报告
2014/06/25 Python
用Python编写生成树状结构的文件目录的脚本的教程
2015/05/04 Python
Python 列表排序方法reverse、sort、sorted详解
2016/01/22 Python
深入浅析Python2.x和3.x版本的主要区别
2018/11/30 Python
浅谈python3.6的tkinter运行问题
2019/02/22 Python
python对象销毁实例(垃圾回收)
2020/01/16 Python
python logging.basicConfig不生效的原因及解决
2020/02/20 Python
Douglas意大利官网:购买香水和化妆品
2020/05/27 全球购物
预备党员思想汇报范文
2013/12/29 职场文书
委托书的格式
2014/08/01 职场文书
2014年党员自我评议总结
2014/09/23 职场文书
2014乡党委副书记党建工作汇报材料
2014/11/02 职场文书
2016年五四青年节校园广播稿
2015/12/17 职场文书
分享一些Java的常用工具
2021/06/11 Java/Android