Inspect docstrings with Pydoc
How come I didn't know about the python -m pydoc command before today!
It lets you inspect the docstrings of any modules, classes, functions, or methods in Python.
I'm running the commands from a Python 3.10 virtual environment but it'll work on any Python version. Let's print out the docstrings of the functools.lru_cache function. Run:
This will print the following on the console:
Works for third party tools as well:
Also, works for any custom Python structure that is accessible from the current Python path. Let's define a function with docstrings and put that in a module called src.py:
You can inspect the entire src.py module or the greetings function specifically as follows:
To inspect the module, run:
To inspect the greetings function only, run:
It'll return:
Instead of inspecting the docstrings one by one, you can also pull up all of them in the current Python path and serve them as HTML pages. To do so, run:
This will render the docstrings as HTML web pages and automatically open the index page with your default browser. From there you can use the built-in search to find and read your ones you need.
Further reading
Discussion in the ATmosphere