dynamically modify help text

Thomas Jollans thomas at jollans.com
Tue Jun 29 07:51:57 EDT 2010


On 06/29/2010 02:37 AM, Ben Finney wrote:
> Brian Blais <bblais at bryant.edu> writes:
> 
>> On Jun 28, 2010, at 14:25 , Chris Rebert wrote:
>>> __doc__ is normally defined on classes, e.g. `A`, not instances,
>>> e.g. `a`. help() looks for __doc__ accordingly.
>>
>> so that gets back to my original question: can I change this text at
>> runtime.  Doesn't look like I can, because it is defined for classes
>> rather than instances.  Am I thinking about this correctly?
> 
> Classes are objects. You can change the ‘__doc__’ attribute of a class
> object the same as you'd change it for any other object::
> 
>     A.__doc__ = "new docstring"
> 

No, you can't. Well, yeah, you can. But you can't. But you can. Ahrgh

I want Python 2.x to go away. It's so inconsistent and silly.

% python2.6
Python 2.6.5+ (release26-maint, Jun 28 2010, 19:46:36)
[GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class OLD: pass
...
>>> class NEW(object): pass
...
>>> OLD.__doc__ = "foo"
>>> NEW.__doc__ = "bar"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: attribute '__doc__' of 'type' objects is not writable
>>>




More information about the Python-list mailing list