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 相关文章推荐
ssh批量登录并执行命令的python实现代码
May 25 Python
Python如何实现守护进程的方法示例
Feb 08 Python
python paramiko模块学习分享
Aug 23 Python
Python批量删除只保留最近几天table的代码实例
Apr 01 Python
Python中Numpy mat的使用详解
May 24 Python
Python使用matplotlib实现交换式图形显示功能示例
Sep 06 Python
PyCharm更改字体和界面样式的方法步骤
Sep 27 Python
Python操作redis和mongoDB的方法
Dec 19 Python
tensorflow之变量初始化(tf.Variable)使用详解
Feb 06 Python
TensorFlow实现保存训练模型为pd文件并恢复
Feb 06 Python
Python自动化测试PO模型封装过程详解
Jun 22 Python
Python基础教程,Python入门教程(超详细)
Jun 24 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
PHP入门
2006/10/09 PHP
PHP syntax error, unexpected $end 错误的一种原因及解决
2008/10/25 PHP
php加密算法之实现可逆加密算法和解密分享
2014/01/21 PHP
学习thinkphp5.0验证类使用方法
2017/11/16 PHP
js操作textarea方法集合封装(兼容IE,firefox)
2011/02/22 Javascript
一个封装js代码-----展开收起效果示例
2013/07/03 Javascript
JS操作Cookie写入和读取实例代码
2013/10/20 Javascript
js简单抽奖代码
2015/01/16 Javascript
JS操作COOKIE实现备忘记录的方法
2016/04/01 Javascript
JQuery的常用选择器、过滤器、方法全面介绍
2016/05/25 Javascript
Vue.js自定义事件的表单输入组件方法
2018/03/08 Javascript
详解vue-cli 脚手架 安装
2019/04/16 Javascript
js常见遍历操作小结
2019/06/06 Javascript
Vue的click事件防抖和节流处理详解
2019/11/13 Javascript
NodeJS开发人员常见五个错误理解
2020/10/14 NodeJs
详解vue 组件的实现原理
2020/11/12 Javascript
python多重继承实例
2014/10/11 Python
实例说明Python中比较运算符的使用
2015/05/13 Python
深入理解python对json的操作总结
2017/01/05 Python
分析python请求数据
2018/08/19 Python
python threading和multiprocessing模块基本用法实例分析
2019/07/25 Python
Django框架ORM数据库操作实例详解
2019/11/07 Python
PyTorch学习:动态图和静态图的例子
2020/01/06 Python
python爬虫把url链接编码成gbk2312格式过程解析
2020/06/08 Python
解决Pycharm双击图标启动不了的问题(JetBrains全家桶通用)
2020/08/07 Python
跨域修改iframe页面内容详解
2019/10/31 HTML / CSS
泰国王权免税店官方网站:KingPower
2019/03/11 全球购物
了解AppleShare protocol(AppleShare协议)吗
2015/08/28 面试题
仪器仪表检测毕业生自荐信
2013/10/31 职场文书
应届生人事助理求职信
2013/11/09 职场文书
家长给孩子的表扬信
2014/01/17 职场文书
艾滋病宣传活动总结
2014/05/08 职场文书
班子成员四风问题自我剖析材料
2014/09/29 职场文书
餐厅保洁员岗位职责
2015/04/10 职场文书
学习雷锋精神倡议书
2015/04/27 职场文书
2016大学优秀学生干部事迹材料
2016/03/01 职场文书