[Tutor] redirecting help -- is this a bad idea?

Brian van den Broek bvande at po-box.mcgill.ca
Sun Aug 1 00:15:07 CEST 2004


Kent Johnson said unto the world upon 31/07/2004 09:13:

> Brian,
> 
> A couple of thoughts:
> 
> - You might consider just making your classes work well with pydoc. The 
> built-in help is introspecting the docstrings for the module you ask for 
> help on. If you write docstrings for your module, built-in help will 
> document it. Take a look at some library modules and the corresponding 
> help to see how this works. If you go this way, your help will be 
> integrated with the help for built-in objects, so your user won't have 
> to figure out which help to use.

Hi Kent,

thanks for the reply.

I started out wanting to be good about docstrings. Then I got sloppy. The 
first time I read my own code a month later, I put being sloppy behind me :-)

In the particular case at hand, I don't want to go the pydoc way--I want a 
very minimal help for a friend who would be completely off-put by the sort 
of things that go in docstrings. I wanted really basic "type this to do 
that" type stuff. Typical docstring messages wouldn't be helpful in this 
case. And I wanted to preserve the builtin help function under another 
name so that I could keep making use of it when I ran the program, too.


> - If you continue down the path you show below, there is no need to 
> define _PyHelper. You can just assign
> pyhelp = help
> 
> Then define your own helper and assign it to help.

OK, let me run that back to see if I follow the intent: Before I make my 
own help, the standard one is present as always. Then

pyhelp = help

assigns pyhelp to that standard help function, and that assignment 
persists even after I have done my thing to make help call my custom function?

If I've got that right, then won't there be the small problem that typing 
pyhelp at the prompt will give the old instructions of help? (As in, it 
will still say "help(object) for help about object", etc.) But that won't 
work as expected, once I've redirected help. I'd thought I needed to use 
the class PyHelper just so that pyhelp would print out the new, customized 
way to get to the pydoc functionality.

Have I misunderstood your point?

Anyway, thanks for the reply.

Best,

Brian vdB

> 
> BTW you are not redefining __builtin__.help when you do this, you are 
> shadowing it with a definition of help in the current global namespace.
> 
> Kent
> 
> At 08:22 PM 7/30/2004 -0400, Brian van den Broek wrote:
> 
>> Hi all,
>>
>> I'm making my first use of classes and also over-riding python builtins.
>> I'd like to run what I am doing by the list as a sanity check and see 
>> if I
>> get a giant *don't do that!* back :-)
>>
>> What I am trying to do is create a set of programs for use by a friend 
>> who
>> is even less computer literate than I. =-O  Part of my aim is to make it
>> self-documenting in an easy to use way. So, I want to redirect the help
>> command to my own help function for my set of programs, while still
>> preserving the normal help behaviour under another name.
>>
>> Having dipped into site.py to see how help was able to work both by 
>> typing
>> "help" and by typing "help()", I saw it was implemented with a class:
>>
>> class _Helper:
>>     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)
>>
>> __builtin__.help = _Helper()
>>
>>
>> I used this as the basis of my redirection of "help". The function that
>> does the work of my help system is tell(). So I've done the following:
>>
>> class _Helper:
>>     def __repr__(self):
>>         return "Type help() for interactive help, " \
>>                "or help(object) for help about object.\n" \
>>                "(For Python's own help function, type pyhelp.)"
>>     def __call__(self, *args, **kwds):
>>         return tell(*args, **kwds)
>>
>> help = _Helper()
>>
>> class _PyHelper:
>>     def __repr__(self):
>>         return "Type pyhelp() for interactive help, " \
>>                "or pyhelp(object) for help about object."
>>     def __call__(self, *args, **kwds):
>>         import pydoc
>>         return pydoc.help(*args, **kwds)
>>
>> pyhelp = _PyHelper()
>>
>>
>> Profoundly wrong or just fine?
>>
>> Best,
>>
>> Brian vdB




More information about the Tutor mailing list