Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 'help()' text in non-interactive manner (Python)

Tags:

python

I'd like to get help(obj) text in IPython or Jupyter notebook in a non-interactive manner, into some variable instead of it being displayed.

Note: help provides more information than obj.__doc__ provides, so that's not quite an alternative.

like image 685
mrkafk Avatar asked Sep 02 '25 04:09

mrkafk


1 Answers

Short answer:

import pydoc
help_result_string = pydoc.render_doc(obj)

Longer answer:

When you call help(obj), it is a wrapper to pydoc.help(obj) (see help.__doc__), which is the same as pydoc.Helper()(obj) (from pydoc source: help = Helper()), which usually leads to pydoc.doc(obj), which writes the resulting string from pydoc.render_doc(obj) to the standard output or a pager, depending on the system.

Details on what other things can happen when help(obj) is called can be found in the pydoc source code.

like image 114
Niklas Mertsch Avatar answered Sep 04 '25 18:09

Niklas Mertsch