Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is imperative mood important for docstrings?

The error code D401 for pydocstyle reads: First line should be in imperative mood.

I often run into cases where I write a docstring, have this error thrown by my linter, and rewrite it -- but the two docstrings are semantically identical. Why is it important to have imperative mood for docstrings?

like image 306
Richard Avatar asked Jan 24 '20 18:01

Richard


People also ask

Why is it important to include docstrings when writing functions?

Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. It's specified in source code that is used, like a comment, to document a specific segment of code.

What should be included in docstrings?

The docstring for a function or method should summarize its behavior and document its arguments and return values. It should also list all the exceptions that can be raised and other optional arguments.

How are docstrings different from regular comments?

A quick recap on comments vs docstrings: Use comments to explain how code works. Comments are great for leaving notes for people working on your program. Docstrings provide documentation about functions, classes, and modules. Use docstrings to teach other developers how to use your program.

What are docstrings What is the use of docstrings in function body?

A docstring is a documentation string at the beginning of the class, function or function body that briefly explains how it works. Every function you create ought to have a docstring. They're in triple-quoted strings and allow for multi-line text.


2 Answers

From the docstring of check_imperative_mood itself:

  """D401: First line should be in imperative mood: 'Do', not 'Does'.

   [Docstring] prescribes the function or method's effect as a command:
    ("Do this", "Return that"), not as a description; e.g. don't write
    "Returns the pathname ...".

(We'll ignore the irony that this docstring itself would fail the test.)

like image 190
chepner Avatar answered Oct 11 '22 15:10

chepner


Why is it important? Because that's the explicit convention for Python docstrings, as detailed in PEP 257. There's nothing particularly special about it - it doesn't seem obvious to me that one of "Multiplies two integers and returns the product" and "Multiply two integers and return the product" is clearly better than the other. But it is explicitly specified in the documentation.

like image 24
Mark Snyder Avatar answered Oct 11 '22 15:10

Mark Snyder