Aan de slagGa gratis aan de slag

Writing docstrings

We just learned some about the benefits of docstrings. In this exercise, you will practice writing docstrings that can be utilized by a documentation generator like Sphinx.

Note that your docstring submission must match the solution exactly. If you find yourself getting it wrong several times, it may be a good idea to refresh the sample code and start over.

Deze oefening maakt deel uit van de cursus

Software Engineering Principles in Python

Cursus bekijken

Oefeninstructies

  • Complete the portions of the docstring that document the parameters.
  • Complete the portion of the docstring describing the return value.
  • Complete the example function usage in the docstring.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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)
Code bewerken en uitvoeren