Dnaiel Dnaiel - 1 year ago 54
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?


Answer Source

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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download