开始使用免费开始使用

编写 docstring

您决定编写一个全球领先的开源自然语言处理 Python 包。它将像 numpy 之于数组、pandas 之于表格数据、scikit-learn 之于机器学习那样,彻底改变处理非结构化文本的方式。

您要实现的第一个函数是 count_letter()。它接收一个字符串和一个单个字母,返回该字母在字符串中出现的次数。为了让开源包的用户能轻松理解这个函数,您需要为它编写 docstring。请按照以下步骤,使用 Google 风格为该函数逐步完善 docstring。

本练习是课程的一部分

Python 函数编写

查看课程

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Add a docstring to count_letter()
def count_letter(content, letter):
  ____
  if (not isinstance(letter, str)) or len(letter) != 1:
    raise ValueError('`letter` must be a single character string.')
  return len([char for char in content if char == letter])
编辑并运行代码