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实现封装得到virustotal扫描结果
Oct 05 Python
Python IDE PyCharm的基本快捷键和配置简介
Nov 04 Python
Django视图和URL配置详解
Jan 31 Python
PyQt5每天必学之事件与信号
Apr 20 Python
详解Python3 基本数据类型
Apr 19 Python
python爬虫豆瓣网的模拟登录实现
Aug 21 Python
python统计函数库scipy.stats的用法解析
Feb 25 Python
python——全排列数的生成方式
Feb 26 Python
pycharm中导入模块错误时提示Try to run this command from the system terminal
Mar 26 Python
快速了解Python开发环境Spyder
Jun 29 Python
python raise的基本使用
Sep 10 Python
如何使用 Flask 做一个评论系统
Nov 27 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
一个可以删除字符串中HTML标记的PHP函数
2006/10/09 PHP
php网页标题中文乱码的有效解决方法
2014/03/05 PHP
destoon实现不同会员组公司名称显示不同的颜色的方法
2014/08/22 PHP
PHP中for循环与foreach的区别
2017/03/06 PHP
TP3.2批量上传文件或图片 同名冲突问题的解决方法
2017/08/01 PHP
在laravel-admin中列表中禁止某行编辑、删除的方法
2019/10/03 PHP
如何用javascript控制上传文件的大小
2006/10/26 Javascript
jquery keypress,keyup,onpropertychange键盘事件
2010/06/25 Javascript
Node.js 异步编程之 Callback介绍(一)
2015/03/30 Javascript
jquery实现的动态回到顶部特效代码
2015/10/28 Javascript
javascript中new关键字详解
2015/12/14 Javascript
[js高手之路]设计模式系列课程-发布者,订阅者重构购物车的实例
2017/08/29 Javascript
Vue结合SignalR实现前后端实时消息同步
2017/09/19 Javascript
JS实现利用两个队列表示一个栈的方法
2017/12/13 Javascript
vue2使用keep-alive缓存多层列表页的方法
2018/09/21 Javascript
vue中组件的3种使用方式详解
2019/03/23 Javascript
vue的注意规范之v-if 与 v-for 一起使用教程
2019/08/04 Javascript
vue el-upload上传文件的示例代码
2020/12/21 Vue.js
python基础教程之面向对象的一些概念
2014/08/29 Python
理解python正则表达式
2016/01/15 Python
python爬取拉勾网职位数据的方法
2018/01/24 Python
Python简单计算文件MD5值的方法示例
2018/04/11 Python
Python使用itertools模块实现排列组合功能示例
2018/07/02 Python
搞清楚 Python traceback的具体使用方法
2019/05/13 Python
Pytorch技巧:DataLoader的collate_fn参数使用详解
2020/01/08 Python
python 计算方位角实例(根据两点的坐标计算)
2020/01/17 Python
Python列表list操作相关知识小结
2020/01/29 Python
基于Python采集爬取微信公众号历史数据
2020/11/27 Python
Python用SSH连接到网络设备
2021/02/18 Python
HTML5样式控制示例代码
2013/11/27 HTML / CSS
英国网上电器商店:Electricshop
2020/03/15 全球购物
SQL里面IN比较快还是EXISTS比较快
2012/07/19 面试题
车间班组长的职责
2013/12/13 职场文书
2014-2015学年工作总结
2014/11/27 职场文书
关于颐和园的导游词
2015/01/30 职场文书
中秋节作文(五年级)之关于月亮
2019/09/11 职场文书