開始使用免費開始

撰寫 docstring

你剛學到使用 docstring 的好處。在這個練習中,你會練習撰寫可供像 Sphinx 這類文件產生器使用的 docstring。

請注意,你送出的 docstring 必須與參考解答「完全一致」。如果你連續多次答錯,建議重設為範例程式碼後重新開始。

本練習屬於課程

Python 的軟體工程原則

檢視課程

練習說明

  • 完成 docstring 中記錄參數的部分。
  • 完成 docstring 中描述回傳值的部分。
  • 在 docstring 中完成函式使用範例。

動手互動練習

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

# Complete the function's docstring
def tokenize(text, regex=r'[a-zA-z]+'):
  """Split text into tokens using a regular expression

  :____ text: text to be tokenized
  :param ____: regular expression used to match tokens using re.findall 
  :____: a list of resulting tokens

  >>> ____('the rain in spain')
  ____
  """
  return re.findall(regex, text, flags=re.IGNORECASE)

# Print the docstring
help(tokenize)
編輯並執行程式碼