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 相关文章推荐
Python3.0与2.X版本的区别实例分析
Aug 25 Python
python黑魔法之参数传递
Feb 12 Python
Python3 Random模块代码详解
Dec 04 Python
Python使用matplotlib绘图无法显示中文问题的解决方法
Mar 14 Python
Python对excel文档的操作方法详解
Dec 10 Python
python使用Plotly绘图工具绘制柱状图
Apr 01 Python
Django REST framework 分页的实现代码
Jun 19 Python
python线程安全及多进程多线程实现方法详解
Sep 27 Python
彻底搞懂 python 中文乱码问题(深入分析)
Feb 28 Python
python 实现 hive中类似 lateral view explode的功能示例
May 18 Python
获取CSDN文章内容并转换为markdown文本的python
Sep 06 Python
python中threading和queue库实现多线程编程
Feb 06 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
追忆往昔!浅谈收音机的百年发展历史
2021/03/01 无线电
php实现获取文章内容第一张图片的方法
2014/11/04 PHP
PHP如何实现Unicode和Utf-8编码相互转换
2015/07/29 PHP
PHP导出带样式的Excel示例代码
2016/08/28 PHP
PHP使用 Imagick 扩展实现图片合成,圆角处理功能示例
2019/09/09 PHP
laravel入门知识点整理
2020/09/15 PHP
Avengerls vs Newbee BO3 第二场2.18
2021/03/10 DOTA
JavaScript初学者应注意的七个细节小结
2012/01/30 Javascript
javascript验证身份证完全方法具体实现
2013/11/18 Javascript
可恶的ie8提示缺少id未定义
2014/03/20 Javascript
javascript中加var和不加var的区别 你真的懂吗
2016/01/06 Javascript
AngularJS 遇到的小坑与技巧小结
2016/06/07 Javascript
CSS3 3D 技术手把手教你玩转
2016/09/02 Javascript
jquery实现图片列表鼠标移入微动
2016/12/01 Javascript
vue.js实例对象+组件树的详细介绍
2017/10/20 Javascript
11行JS代码制作二维码生成功能
2018/03/09 Javascript
Vue使用mixins实现压缩图片代码
2018/03/14 Javascript
详解如何使用webpack打包JS
2018/06/21 Javascript
详解vue几种主动刷新的方法总结
2019/02/19 Javascript
vue计算属性无法监听到数组内部变化的解决方案
2019/11/06 Javascript
JavaScript碰撞检测原理及其实现代码
2020/03/12 Javascript
vue 封装 Adminlte3组件的实现
2020/03/18 Javascript
Vue实现简单的跑马灯
2020/05/25 Javascript
Python3将jpg转为pdf文件的方法示例
2019/12/13 Python
python3 动态模块导入与全局变量使用实例
2019/12/22 Python
Python pip使用超时问题解决方案
2020/08/03 Python
python+selenium实现12306模拟登录的步骤
2021/01/21 Python
从零实现一个自定义html5播放器的示例代码
2017/08/01 HTML / CSS
德国家具、照明、家居用品网上商店:Wayfair.de
2020/02/13 全球购物
高三学生评语大全
2014/04/25 职场文书
自主招生推荐信范文
2014/05/10 职场文书
幼儿园教师节演讲稿
2014/09/03 职场文书
技术员个人工作总结
2015/03/03 职场文书
夫妻吵架保证书
2015/05/08 职场文书
mysql中between的边界,范围说明
2021/06/08 MySQL
讨论nginx location 顺序问题
2022/05/30 Servers