Dnaiel Dnaiel - 2 months ago 5
Python Question

assign output of help to a variable instead of stdout in python

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!

Answer

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()