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

創(chuàng)新互聯(lián)建站成都網(wǎng)站建設(shè)按需策劃,是成都網(wǎng)站推廣公司,為LED顯示屏提供網(wǎng)站建設(shè)服務(wù),有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設(shè)計服務(wù):原型圖制作、網(wǎng)站創(chuàng)意設(shè)計、前端HTML5制作、后臺程序開發(fā)等。成都網(wǎng)站推廣熱線:028-86922220
>>> def sayhi():
"""
用該函數(shù)打印Hi
"""
print("Hi")
>>> sayhi()
Hi要想獲取一個函數(shù)的docstring,我們使用它的_doc_屬性
>>> sayhi.__doc__ ‘ This function prints Hi ’
相關(guān)推薦:《python基礎(chǔ)教程》
docstring,不僅可以編寫代碼,同時也能寫出文檔,保持代碼和文檔的一致。
docstring說白了就是一堆代碼中的注釋。
Python的docstring可以通過help函數(shù)直接輸出一份有格式的文檔。
編寫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
標(biāo)題名稱:創(chuàng)新互聯(lián)Python教程:python docstring是什么
網(wǎng)站地址:http://fisionsoft.com.cn/article/dhpejih.html


咨詢
建站咨詢
