天天看點

python doc_python中的_doc_是什麼

文檔字元串。注意,是 __doc__ ,前後各兩個下劃線。

一般而言,是對函數/方法/子產品所實作功能的簡單描述。但當指向具體對象時,會顯示此對象從屬的類型的的文檔字元串。(示例見以下 a.__doc__)

>>> str.__doc__

"str(string[, encoding[, errors]]) -> str\n\nCreate a new string object from the given encoded string.\nencoding defaults to the current default string encoding.\nerrors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."

>>> import math

>>> math.__doc__

'This module is always available. It provides access to the\nmathematical functions defined by the C standard.'

>>> a = [1]

>>> a.count.__doc__

'L.count(value) -> integer -- return number of occurrences of value'

>>> a.__doc__

"list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items"

為自定義的函數建立 __doc__ 的方法示例:

>>> def func():

"""Here's a doc string"""

pass

>>> func.__doc__

"Here's a doc string"

更詳細的資料請參考 Python Tutorial 4.7.6 Documentation Strings.