開始使用免費開始

審查同事的程式碼

你的同事請你幫忙審查他們寫的程式碼,並提供一些建議,讓它能夠準備好上線。你知道為了讓函式可維護且可重複使用,撰寫 docstring 是最佳實務。因此,為了檢查一下,你決定把這個 has_docstring() 函式套用到他們所有的函式上。

def has_docstring(func):
  """Check to see if the function 
  `func` has a docstring.

  Args:
    func (callable): A function.

  Returns:
    bool
  """
  return func.__doc__ is not None

本練習屬於課程

Python 函式寫作

檢視課程

動手互動練習

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

# Call has_docstring() on the load_and_plot_data() function
ok = has_docstring(____)

if not ok:
  print("load_and_plot_data() doesn't have a docstring!")
else:
  print("load_and_plot_data() looks ok")
編輯並執行程式碼