MyWiki:Reference desk/Archives/Computing/2014 November 16

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

This template must be substituted. Replace {{Archive header with {{subst:Archive header.

{| width = "100%"

|- ! colspan="3" align="center" | Computing desk |- ! width="20%" align="left" | < November 15 ! width="25%" align="center"|<< Oct | November | Dec >> ! width="20%" align="right" |Current desk > |}

Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


November 16

[edit source]

a = help(list.append) and help(list.append)

[edit source]

Why are both method calls the same? Can't the 1st call save the help()? I see that help() returns None, but, what if I wanted to save the text of the help()?--Senteni (talk) 17:02, 16 November 2014 (UTC)

Help prints the docstring, which is a property of the object called __doc__
So you'd do
   a = list.append.__doc__
-- Finlay McWalterTalk 20:22, 16 November 2014 (UTC)
More generally (and for more complicated problems where there isn't a simple string like this to look at) you can capture your programs own prints (to stdout) by temporarily substituting a StringIO object in for sys.stderr. This is overkill for your current problem, but might come in handy later. An example (which accomodates the changes to StringIO done between python2 and python3) is:
import sys

try:
    import cStringIO
    new_stdout = cStringIO.StringIO()
except ImportError:
    import io
    new_stdout = io.StringIO()

old_stdout = sys.stdout

sys.stdout = new_stdout
help(list.append) # stuff that would normally print to stdout, usually the terminal
a = sys.stdout.getvalue()
sys.stdout.close()
sys.stdout = old_stdout

print("the output of help was >>{:s}<<".format(a))
-- Finlay McWalterTalk 21:06, 16 November 2014 (UTC)