3

I discovered the built-in help() recently that prints some information for a module, function, method, class, etc. But where exactly does it find the information it shows? Python docs don't give a single hint about this.

>>> import base64
>>> help(base64)
Help on module base64:

NAME
    base64 - RFC 3548: Base16, Base32, Base64 Data Encodings

FILE
    /usr/lib/python2.7/base64.py
..
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Pithikos
  • 18,827
  • 15
  • 113
  • 136
  • http://stackoverflow.com/questions/5430020/python-how-to-get-information-about-a-function http://stackoverflow.com/questions/139180/listing-all-functions-in-a-python-module – Mustafa Toker Feb 24 '15 at 14:41

3 Answers3

6

If you simply do, help(help), you will get

Help on _Helper in module site object:

class _Helper(__builtin__.object)
 |  Define the builtin 'help'.
 |  This is a wrapper around pydoc.help (with a twist).
 |  
 |  Methods defined here:
 |  
 |  __call__(self, *args, **kwds)
 |  
 |  __repr__(self)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

basically, help gets its input from pydoc.help. Quoting, pydoc documentation,

For modules, classes, functions and methods, the displayed documentation is derived from the docstring (i.e. the __doc__ attribute) of the object, and recursively of its documentable members. If there is no docstring, pydoc tries to obtain a description from the block of comment lines just above the definition of the class, function or method in the source file, or at the top of the module (see inspect.getcomments()).

The built-in function help() invokes the online help system in the interactive interpreter, which uses pydoc to generate its documentation as text on the console.


But where exactly does it find the information it shows?

The quoted bold text above answers this question.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
2

It is in site.py, if you use help.__class__ you will see it is site._Helper which is just a wrapper around pydoc.help :

In [1]: help.__class__
Out[1]: site._Helper

_Helper class from site.py:

class _Helper(object):
    """Define the builtin 'help'.
    This is a wrapper around pydoc.help (with a twist).

    """

    def __repr__(self):
        return "Type help() for interactive help, " \
               "or help(object) for help about object."
    def __call__(self, *args, **kwds):
        import pydoc
        return pydoc.help(*args, **kwds)

def sethelper():
    __builtin__.help = _Helper() 

help(object) is equivalent to __builtin__.help(object) which passes the object to pydoc.help.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

If you create the following structure

C (directory) --> __init__.py (file in this directory)

Then write the following into __init__.py

'''Some help'''

And run help(C). You'll be shown the following

Help on package C:

NAME
   C - Some help

FILE
   /C/__init__.py

PACKAGE CONTENTS

In that case help() gets the help from the docstring (the one between three ''s)

ForceBru
  • 43,482
  • 10
  • 63
  • 98