[Python-Dev] Python 2.1 PEPs

Ka-Ping Yee ping@lfw.org
Tue, 17 Apr 2001 12:13:36 -0700 (PDT)


On Tue, 17 Apr 2001, Paul Prescod wrote:
> Right, but the bootstrap was always part of the proposal!

Right.

> All it would take now is
> 
> class help:
>    def __repr__(self):
>       import pydoc
>       __builtins__.help = pydoc.help
>       repr(__builtins__.help)

Yes, pretty much.  I suggested something to that effect on Friday
night.  (Guido decided it was too late in the game to change site.py
at that point.)  Here was my proposed addition to site.py:

    # Define the built-in object "help" to provide interactive help.
    class Helper:
        def __repr__(self):
            import inspect
            if inspect.stack()[1][3] == '?': # not called from a function
                self()
                return ''
            return '<Helper instance>'
        def __call__(self, arg=None):
            import pydoc
            pydoc.help(arg)
    __builtin__.help = Helper()

Why the strange check involving inspect.stack()?
inspect.stack()[1][3] is the co_name of the parent frame.
If we check that the __repr__ is not being requested by a
function call, everything works perfectly in IDLE as well
as the plain interpreter, and the helper object is still safe
to pass around.  Nothing breaks even if you say help(help).

The above few lines are a reasonable addition to sitecustomize.py
if you happen to be setting up a local installation of Python.


-- ?!ng

"If I have seen farther than others, it is because I was standing on a
really big heap of midgets."
    -- K. Eric Drexler