国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中的多行注釋文檔編寫風格匯總

2019-11-25 16:43:28
字體:
來源:轉載
供稿:網友

什么是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 paramReturns:  This is a description of what is returnedRaises:  KeyError - raises an exception"""

Numpydoc (Numpy風格)

"""My numpydoc description of a kindof very exhautive numpydoc format docstring.Parameters----------first : array_like  the 1st param name `first`second :  the 2nd paramthird : {'value', 'other'}, optional  the 3rd param, by default 'value'Returns-------string  a value in a stringRaises------KeyError  when a key errorOtherError  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.
看效果:
2016616165000863.jpg (1189×764)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 故城县| 绵阳市| 台江县| 顺平县| 左权县| 名山县| 巩义市| 嘉义市| 尉犁县| 巴彦淖尔市| 新营市| 凌源市| 万安县| 博爱县| 宾阳县| 柳江县| 苏州市| 依安县| 乌什县| 三门峡市| 开化县| 祁连县| 商南县| 汤原县| 永胜县| 沈阳市| 丰城市| 崇明县| 新野县| 邹城市| 修武县| 嵊泗县| 湖南省| 淮安市| 东乌珠穆沁旗| 渝中区| 哈巴河县| 长春市| 成安县| 抚顺市| 怀宁县|