溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python docstring是什么意思

發(fā)布時(shí)間:2020-09-21 14:06:47 來源:億速云 閱讀:532 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關(guān)python docstring是什么意思,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

Docstring是一種文檔字符串,用于解釋構(gòu)造的作用。我們?cè)诤瘮?shù)、類或方法中將它放在首位來描述其作用。我們用三個(gè)單引號(hào)或雙引號(hào)來聲明docstring。

>>> def sayhi():
 """
用該函數(shù)打印Hi
"""
 print("Hi") 
>>> sayhi()
Hi

要想獲取一個(gè)函數(shù)的docstring,我們使用它的_doc_屬性

>>> sayhi.__doc__
‘
  This function prints Hi
’

docstring,不僅可以編寫代碼,同時(shí)也能寫出文檔,保持代碼和文檔的一致。

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

以上就是python docstring是什么意思,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI