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网络爬虫项目:内容提取器的定义
Oct 25 Python
对pandas中apply函数的用法详解
Apr 10 Python
python pandas修改列属性的方法详解
Jun 09 Python
Python实现将Excel转换成为image的方法
Oct 23 Python
python实现抖音点赞功能
Apr 07 Python
Django数据库类库MySQLdb使用详解
Apr 28 Python
pyqt5 从本地选择图片 并显示在label上的实例
Jun 13 Python
python实现贪吃蛇游戏源码
Mar 21 Python
在django admin中配置搜索域是一个外键时的处理方法
May 20 Python
Django使用django-simple-captcha做验证码的实现示例
Jan 07 Python
使用tensorflow 实现反向传播求导
May 26 Python
基于Python编写一个监控CPU的应用系统
Jun 25 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 url地址栏传中文乱码解决方法集合
2010/06/25 PHP
小文件php+SQLite存储方案
2010/09/04 PHP
php操作xml入门之xml标签的属性分析
2015/01/23 PHP
PHP Swoole异步Redis客户端实现方法示例
2019/10/24 PHP
Laravel框架实现即点即改功能的方法分析
2019/10/31 PHP
jQuery 常见开发使用技巧总结
2009/12/26 Javascript
基于jQuery的投票系统显示结果插件
2011/08/12 Javascript
对javascript的一点点认识总结《javascript高级程序设计》读书笔记
2011/11/30 Javascript
javascript表单验证 - Parsley.js使用和配置
2013/01/25 Javascript
详解JavaScript函数绑定
2013/08/18 Javascript
Js实现动态添加删除Table行示例
2014/04/14 Javascript
浅谈JS中的bind方法与函数柯里化
2016/08/10 Javascript
基于代数方程库Algebra.js解二元一次方程功能示例
2017/06/09 Javascript
vue中如何实现变量和字符串拼接
2017/06/19 Javascript
探索Vue高阶组件的使用
2018/01/08 Javascript
详解webpack模块化管理和打包工具
2018/04/21 Javascript
vue+Element-ui前端实现分页效果
2020/11/15 Javascript
[49:29]LGD vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
python 查找文件夹下所有文件 实现代码
2009/07/01 Python
用Python实现一个简单的线程池
2015/04/07 Python
django model去掉unique_together报错的解决方案
2016/10/18 Python
python中解析json格式文件的方法示例
2017/05/03 Python
Python编程之Re模块下的函数介绍
2017/10/28 Python
python_opencv用线段画封闭矩形的实例
2018/12/05 Python
django 外键model的互相读取方法
2018/12/15 Python
python爬虫中多线程的使用详解
2019/09/23 Python
python实现差分隐私Laplace机制详解
2019/11/25 Python
python实现上传文件到linux指定目录的方法
2020/01/03 Python
python学生管理系统的实现
2020/04/05 Python
python爬虫基础之urllib的使用
2020/12/31 Python
Html5 localStorage入门教程
2018/04/26 HTML / CSS
计算机专业毕业生求职信
2014/04/30 职场文书
小学英语复习计划
2015/01/19 职场文书
建筑工程材料员岗位职责
2015/04/11 职场文书
初中毕业生感言
2015/07/31 职场文书
2019运动会广播加油稿汇总
2019/08/21 职场文书