新聞中心
這里有您想知道的互聯網營銷解決方案
創(chuàng)新互聯Python教程:python docstring是什么
docstring是一種文檔字符串,用于解釋構造的作用。我們在函數、類或方法中將它放在首位來描述其作用。我們用三個單引號或雙引號來聲明docstring。

創(chuàng)新互聯建站成都網站建設按需策劃,是成都網站推廣公司,為LED顯示屏提供網站建設服務,有成熟的網站定制合作流程,提供網站定制設計服務:原型圖制作、網站創(chuàng)意設計、前端HTML5制作、后臺程序開發(fā)等。成都網站推廣熱線:028-86922220
>>> def sayhi():
"""
用該函數打印Hi
"""
print("Hi")
>>> sayhi()
Hi要想獲取一個函數的docstring,我們使用它的_doc_屬性
>>> sayhi.__doc__ ‘ This function prints Hi ’
相關推薦:《python基礎教程》
docstring,不僅可以編寫代碼,同時也能寫出文檔,保持代碼和文檔的一致。
docstring說白了就是一堆代碼中的注釋。
Python的docstring可以通過help函數直接輸出一份有格式的文檔。
編寫test.py
def printMax(x, y): '''Prints the maximum of two numbers. The two values must be integers.''' x = int(x) # convert to integers, if possible y = int(y) if x > y: print (x, 'is maximum') else: print (y, 'is maximum') printMax(3, 5) print (printMax.__doc__)
命令行輸入 help(test)
import test 5 is maximum Prints the maximum of two numbers. The two values must be integers. help(test) Help on module test: NAME test - Created on Sat Jun 2 19:05:08 2018 DESCRIPTION @author: linzhiwei02 FUNCTIONS printMax(x, y) Prints the maximum of two numbers. The two values must be integers. FILE /Users/linzhiwei02/Desktop/test.py
名稱欄目:創(chuàng)新互聯Python教程:python docstring是什么
網頁地址:http://m.5511xx.com/article/dhpejih.html


咨詢
建站咨詢
