Docstring
PronunciationDOK-string
Definition
A docstring is a string placed right at the start of a module, class or function that describes what it does. Tools and help() read it.
Where you hear it
In Python code reviews, documentation tools, and style guides that ask every public function to explain itself.
Examples
Add a docstring that says what the function returns and when it raises.
help(my_function)prints the docstring.The docstring lists the arguments, so the editor shows them when you type the call.
Common mistake
Repeating the code in words. A good docstring explains the purpose and the surprising cases, not each line.
Don't confuse with
A comment, which starts with # and is ignored by the program. A docstring is real data attached to the object.
Say it at work
- In a meeting or chat
Please add a docstring to every public function.
- In an email or ticket
The docstring explains the edge case better than a comment.
Keep exploring
More in Programming Fundamentals