Dnaiel Dnaiel - 9 months ago 32
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?



You can capture stdout while help(myclass) runs:

from cStringIO import StringIO
import sys

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


sys.stdout = stdout

myhelp = buffer.getvalue()