3

I want to do something like this in the python interpreter.

myhelp = help(myclass)

but the output goes to stdout. Is it possible to assign it to a variable?

Thanks!

2
  • 1
    Use pydoc... import pydoc; myhelp = pydoc.render_doc(myclass) Commented Sep 24, 2016 at 1:10
  • @AChampion I can't seem to find a question related to this, maybe it would be good to post your comment as an answer since it is a pretty good concise way to actually store the help data. For anyone else looking to do this, would be helpful as an answer. Commented Sep 24, 2016 at 1:19

1 Answer 1

4

You can capture stdout while help(myclass) runs:

from cStringIO import StringIO
import sys

stdout = sys.stdout
buffer = StringIO()
sys.stdout = buffer

help(myclass)

sys.stdout = stdout

myhelp = buffer.getvalue()
Sign up to request clarification or add additional context in comments.

1 Comment

Just wanted to add the Python 3 friendly support for this. That to use StringIO, you have to import from io -> from io import StringIO

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.