20

Is there an option to print the output of help('myfun'). The behaviour I'm seeing is that output is printed to std.out and the script waits for user input (i.e. type 'q' to continue).

There must be a setting to set this to just dump docstrings.

Alternatively, if I could just dump the docstring PLUS the "def f(args):" line that would be fine too.

Searching for "python help function" is comical. :) Maybe I'm missing some nice pydoc page somewhere out there that explains it all?

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
mathtick
  • 6,487
  • 13
  • 56
  • 101

4 Answers4

26

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.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • Other answers are probably correct but this is exactly what I needed. – mathtick Oct 02 '11 at 20:48
  • @mathtick: Now that's what I call conscientious, accepting an answer after this long! – kindall Oct 03 '11 at 14:37
  • Ha ... I guess it's as testament to the stackoverflow interface helping me with my scattered work life! I was perusing my profile, actually paid attention to my list of questions and noticed a few that I had forgotten to accept. – mathtick Oct 03 '11 at 16:09
6

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

Jürgen Strobel
  • 2,200
  • 18
  • 30
  • For some reason that I yet ignore, this is generally different from the result of `pydoc.render_doc(obj)`—I guess notably in the case of a function defined inside another function, that sets its docstring by setting `func.__doc__ = …`. – Eric O. Lebigot Feb 14 '17 at 11:44
5

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.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • co_varnames will show a lot more than just the input params: def foo(a,b,c): d = "foo" e = "bar" print foo.func_code.co_varnames ('a', 'b', 'c', 'd', 'e') – MattK May 29 '18 at 16:13
2
>>> x = 2
>>> x.__doc__
'int(x[, base]) -> integer\n\nConvert a string or number to an integer, if possi
ble.  A floating point\nargument will be truncated towards zero (this does not i
nclude a string\nrepresentation of a floating point number!)  When converting a
string, use\nthe optional base.  It is an error to supply a base when converting
 a\nnon-string. If the argument is outside the integer range a long object\nwill
 be returned instead.'

Is that what you needed?

edit - you can print(x.__doc__) and concerning the function signature, you can build it using the inspect module.

>>> inspect.formatargspec(inspect.getargspec(os.path.join))
'((a,), p, None, None)'
>>> help(os.path.join)
Help on function join in module ntpath:

join(a, *p)
    Join two or more pathname components, inserting "\" as needed
ubershmekel
  • 11,864
  • 10
  • 72
  • 89