Docstrings in Python are special text used to describe what a function, class, or module does in your Python code. They are written inside triple quotes and after the definition. Docstrings help you understand the purpose of the Python code without the need to read every line. Unlike regular comments, docstrings can be accessed using the help() function. Using the docstrings makes the code clean and readable. In this blog, you will learn the implementation, formatting, and application of docstrings in detail with examples.
Table Of Contents
What are Docstrings in Python?
In this section, you will be introduced to docstrings and how they differ from your general Python comments. Docstrings, or documentation strings, are literal strings that are kept as the first statement in a function, class, or module. Comments are completely ignored when the program runs, but docstrings are saved inside a special attribute called __doc__ and can be used in the program.
For Example:
Output:
Explanation: The function will print a greeting and return its documentation through the __doc__. This shows us that Python stores the docstrings for accessing and usage later.
Why Use Docstrings in Python?
Docstrings in Python help you understand what every component of your code performs without going into the implementation. They will provide metadata for the tools for documentation and powerful IDE features, for example, hover-to-view help. It will also integrate seamless tools such as help() and pydoc.
Example:
Output:
Explanation: Here, the help function returns and displays the docstring along with the structured documentation without reading the source code.
Types of Python Docstrings
In this section, you will be informed regarding the various types of docstrings that the Python language supports for modules, functions, and classes.
1. Module Docstring in Python
The module-level docstring in Python describes the functionality provided by a Python file.
Example:
Explanation: Here, Docstrings at the module level summarize the file’s purpose and are typically placed at the very top of the .py file. They can be accessed using the code import math_utils, and in the next line, write print(math_utils.__doc__).
2. Function/Method Docstrings
The function in Docstrings defines what the function does, expected parameters, return values, and exceptions.
For example:
Output:
Explanation: Here, the function would include a properly formatted docstring with detailed inputs, outputs, and the function’s core purpose.
3. Class Docstrings in Python
The class docstrings in Python help to describe the role and the capabilities of the class.
Example:
Output:
Explanation: Here, the class and the method docstrings both can be retrieved, giving context at both structural levels.
Features of Docstrings in Python
This section outlines the capabilities of utilizing docstrings and the advantages of using docstrings over traditional comments in Python.
- Multiline and Structured: Triple quotes allow line breaks, giving freedom to write detailed explanations.
- Runtime Accessibility: Docstrings are stored in __doc__ and can be retrieved using introspection tools.
- Documentation Tool Integration: Tools in Python, with the likes of Sphinx, pdoc, and help(), can parse and display docstrings.
- Support for Formatting Styles: Formats like Google style, NumPy style, and reStructuredText provide consistency.
- Useful in IDEs: Most modern IDEs show docstrings in tooltips, making them immediately useful during development.