dynamically modify help text

Chris Rebert clp2 at rebertia.com
Mon Jun 28 14:25:39 EDT 2010


On Mon, Jun 28, 2010 at 11:13 AM, Brian Blais <bblais at bryant.edu> wrote:
> On Jun 27, 2010, at 22:37 , Red Forks wrote:
>> Read you doc file and set the __doc__ attr of the object you want to
>> change.
>>
>> On Monday, June 28, 2010, Brian Blais <bblais at bryant.edu> wrote:
>>> I know that the help text for an object will give a description of every
>>> method based on the doc string.  Is there a way to add something to this
>>> text, specific to an object, but generated at run-time?  I have an object
>>> that reads a file, and I would like part of that file to be shown if one
>>> does:  help(myobject)
>
> python file:
>
> #=============
> class A(object):
>    pass
>
> help_text="this is some text"
>
> a=A()
> a.__doc__=help_text
> #=============
>
> in ipython:
>
> shows up here:
>
> In [5]:a?
> Type:           A
> Base Class:     <class '__main__.A'>
> String Form:    <__main__.A object at 0x13270f0>
> Namespace:      Interactive
> File:
> /Library/Frameworks/Python.framework/Versions/5.0.0/lib/python2.5/site-packages/IPython/FakeModule.py
> Docstring:
>    this is some text
>
>
> but not with the help command:
>
> In [6]:help(a)
> Help on A in module __main__ object:
>
> class A(__builtin__.object)
>  |  Data descriptors defined here:
>  |
>  |  __dict__
>  |      dictionary for instance variables (if defined)
>  |
>  |  __weakref__
>  |      list of weak references to the object (if defined)
>
>
> also does the same thing with the regular python prompt.
>
> is there a reason for this?

__doc__ is normally defined on classes, e.g. `A`, not instances, e.g.
`a`. help() looks for __doc__ accordingly.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list