iT邦幫忙

2021 iThome 鐵人賽

DAY 28
0
自我挑戰組

Python淺顯易懂的小教室系列 第 28

[Day_28]函式與遞迴_(7)

函式的說明文件

可以在函式下方使用「'''」撰寫函式的說明文件,說明文件可以跨好幾行,直到找到下一個「'''」,使用「'''」會保留第2行以後所有開頭的空格,如以下範例。

def min(a,b):
    '''使用min可以找出a與b較小的值
    Args:
        a:輸入的第一個參數
        b:輸入的第二個參數
    Returns:
        回傳a與b中較小的值
    '''
    if a>b:
        return b
    else :
        return a

使用「help(min)」可以讀取函式的說明文件,如下。

Help on built-in function min in module builtins:

min(...)
    min(iterable, *[, default=obj, key=func]) -> value
    min(arg1, arg2, *args, *[, key=func]) -> value
    
    With a single iterable argument, return its smallest item. The
    default keyword-only argument specifies an object to return if
    the provided iterable is empty.
    With two or more arguments, return the smallest argument.

使用「print(min.doc)」可以讀取函式的說明文件,如下。

min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value

With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.

我們可以善用函式的說明文件,讓後續維護程式的程式設計師可以快速了解函式的用途與功能。

明天會繼續介紹函式喔~~
大家加油~~
/images/emoticon/emoticon08.gif


上一篇
[Day_27]函式與遞迴_(6)
下一篇
[Day_29]函式與遞迴_(8)
系列文
Python淺顯易懂的小教室30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言