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端口扫描系统实现方法
Nov 19 Python
python检测远程udp端口是否打开的方法
Mar 14 Python
处理Python中的URLError异常的方法
Apr 30 Python
Python3 伪装浏览器的方法示例
Nov 23 Python
python 动态加载的实现方法
Dec 22 Python
linux下python使用sendmail发送邮件
May 22 Python
Python3之手动创建迭代器的实例代码
May 22 Python
Python操作excel的方法总结(xlrd、xlwt、openpyxl)
Sep 02 Python
Python requests模块cookie实例解析
Apr 14 Python
Python3.9 beta2版本发布了,看看这7个新的PEP都是什么
Jun 10 Python
python如何对链表操作
Oct 10 Python
python-jwt用户认证食用教学的实现方法
Jan 19 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的变量总结 新手推荐
2011/04/18 PHP
php数组去重的函数代码
2013/02/03 PHP
基于php在各种web服务器的运行模式详解
2013/06/03 PHP
windows下安装php的memcache模块的方法
2015/04/07 PHP
PHP htmlspecialchars() 函数实例代码及用法大全
2018/09/18 PHP
Eval and new funciton not the same thing
2012/12/27 Javascript
JavaScript建立一个语法高亮输入框实现思路
2013/02/26 Javascript
js 删除数组的几种方法小结
2014/02/21 Javascript
javascript密码强度校验代码(两种方法)
2015/08/10 Javascript
javascript实现方法调用与方法触发小结
2016/03/26 Javascript
学习vue.js条件渲染
2016/12/03 Javascript
js实现可以点击收缩或张开的悬浮窗
2017/09/18 Javascript
jquery实现点击a链接,跳转之后,该a链接处显示背景色的方法
2018/01/18 jQuery
JS浅拷贝和深拷贝原理与实现方法分析
2019/02/28 Javascript
微信小程序中的video视频实现 自定义播放按钮、封面图、视频封面上文案
2020/01/02 Javascript
使用python实现strcmp函数功能示例
2014/03/25 Python
Python中利用sorted()函数排序的简单教程
2015/04/27 Python
DataFrame 将某列数据转为数组的方法
2018/04/13 Python
Python利用字典破解WIFI密码的方法
2019/02/27 Python
通过celery异步处理一个查询任务的完整代码
2019/11/19 Python
Python类中self参数用法详解
2020/02/13 Python
利用python在excel中画图的实现方法
2020/03/17 Python
解决python3输入的坑——input()
2020/12/05 Python
HTML 5.1来了 9月份正式发布 更新内容预览
2016/04/26 HTML / CSS
倩碧香港官方网站:Clinique香港
2017/11/13 全球购物
彪马加拿大官网:PUMA加拿大
2018/10/04 全球购物
DOUGLAS荷兰:购买香水和化妆品
2020/10/24 全球购物
会计电算化专业个人的自我评价
2013/11/24 职场文书
母婴店促销方案
2014/03/05 职场文书
大学新学期计划书
2014/04/28 职场文书
关键在于落实心得体会
2014/09/03 职场文书
个人违纪检讨书
2014/09/15 职场文书
党的群众路线教育实践活动调研报告
2014/11/03 职场文书
教师专业技术工作总结2015
2015/05/13 职场文书
一文了解JavaScript用Element Traversal新属性遍历子元素
2021/11/27 Javascript
JAVA长虹键法之建造者Builder模式实现
2022/04/10 Java/Android