python "help" function: printing docstrings

To get exactly the help that's printed by help(str) into the variable strhelp:

import pydoc
strhelp = pydoc.render_doc(str, "Help on %s")

Of course you can then easily print it without paging, etc.


You've already seen reference to the docstring, the magic __doc__ variable which holds the body of the help:

def foo(a,b,c): 
   ''' DOES NOTHING!!!! '''
   pass

print foo.__doc__ # DOES NOTHING!!!!

To get the name of a function, you just use __name__:

def foo(a,b,c): pass

print foo.__name__ # foo

The way to get the signature of a function which is not built in you can use the func_code property and from that you can read its co_varnames:

def foo(a,b,c): pass
print foo.func_code.co_varnames # ('a', 'b', 'c')

I've not found out how to do the same for built in functions.


If you want to access the raw docstring from code:

   myvar = obj.__doc__
   print(obj.__doc__)

The help function does some additional processing, the accepted answer shows how to replicate this with pydoc.render_doc().