python整理筆記3---__doc__(文件字串)
阿新 • • 發佈:2019-01-28
1、定義
如果將一個字串作為函式的第一部分,而沒有名稱去引用它,python將它儲存在函式中,以便以後可以引用它,這個字串通常叫做docstring,是documentation string(文件字串)的縮寫。簡單的說就是給函式一個描述。
2、顯示<span style="font-size:24px;">def add(a,b): """this a function to additon""" #文件字串 sum = a+b return sum</span><span style="font-size:18px; "> </span>
文件字串通過函式名稱後加英文句點顯示
正確顯示:
<span style="font-size:24px;">>>> print("%s" % add.__doc__)
this a function to additon
>>> </span>
錯誤顯示:
注意:__doc__前後各有兩個下劃線<span style="font-family:SimSun;">>>> print("%s" % add._doc_) Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> print("%s" % add._doc_) AttributeError: 'function' object has no attribute '_doc_' >>> </span>