開始使用免費開始

擷取 docstring

你和一群朋友正在開發一套超好用的 Python IDE(整合式開發環境,例如 PyCharm、Spyder、Eclipse、Visual Studio 等)。團隊想加入一個功能:當使用者開始輸入函式名稱時,顯示包含該函式 docstring 的工具提示。這樣使用者就不必另外查文件,就能快速看到想用的函式說明。現在你被指派完成 build_tooltip() 函式,用來從任意函式擷取 docstring。

你會重用上一個練習中寫好的 count_letter() 函式,來示範我們可以正確擷取它的 docstring。

本練習屬於課程

Python 函式寫作

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Get the "count_letter" docstring by using an attribute of the function
docstring = ____.____

border = '#' * 28
print('{}\n{}\n{}'.format(border, docstring, border))
編輯並執行程式碼