激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python中的多行注釋文檔編寫風格匯總

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

2020-08-27 12:28mattkang Python

在Python中利用多行注釋編寫小型的程序文檔說明非常方便,而約定俗成的格式也多種多樣,這里我們就進行一下最常見的Python中的多行注釋文檔編寫風格匯總:

什么是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中的多行注釋文檔編寫風格匯總

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 九色com| 一级黄色免费观看 | 亚洲爱爱网站 | 国产黄色一区二区 | 免费黄色av | 91成人一区二区三区 | 亚州欧美视频 | 日本成年网| 国产高潮失禁喷水爽到抽搐视频 | h视频在线免费观看 | 国产精品免费久久久久 | 51国产偷自视频区视频小蝌蚪 | 久久爽久久爽久久av东京爽 | 日本在线一区二区 | 日本视频免费看 | 91精品国产综合久久婷婷香 | 久久久精品视频免费 | 久久久久久久一区二区 | av电影院在线观看 | 中文字幕精品久久 | 一级电影免费在线观看 | 精品国产一级毛片 | 人人玩人人爽 | 亚洲成人在线免费 | 美国黄色毛片女人性生活片 | 国产成人羞羞视频在线 | 看91| 亚洲欧美国产精品va在线观看 | 欧美色大成网站www永久男同 | 日本精品中文字幕 | 欧美日韩亚洲视频 | 一级片在线观看 | 极品xxxx欧美一区二区 | 国产91亚洲精品一区二区三区 | 日韩av在线网 | 羞羞的视频在线 | 成人一级毛片 | 91在线视频在线观看 | jizzjizz中国少妇中文 | 日韩在线播放中文字幕 | 亚洲福利视|