Inheriting methods but over-riding docstrings

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Jan 18 15:11:46 CET 2010


En Sun, 17 Jan 2010 23:23:45 -0300, Steve Holden <steve at holdenweb.com>  
escribió:
> Gabriel Genellina wrote:

>> Methods don't have docstrings; functions do. So one has to "clone" the
>> function to set a new docstring.
>>
> On behalf of all methods I would like to say "We demand the right to
> docstrings".
>
>>>> print abs.__doc__
> abs(number) -> number
>
> Return the absolute value of the argument.
>>>>
>
> Perhaps I am misunderstanding "don't have docstrings". This code:

Sorry, I wasn't clear at all. What I wanted to say is that docstrings are  
actually stored as *function* attributes; methods inherit the docstring  
 from the function they are built on (as a read-only attribute).


py> class Base(object):
...   def method(self):
...     "my docstring"
...
py> Base.method.__doc__
'my docstring'
py> Base.method.__doc__ = "another docstring"
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: attribute '__doc__' of 'instancemethod' objects is not  
writable
py> Base.method.im_func.__doc__
'my docstring'
py> Base.method.im_func.__doc__ = "another docstring"
py> Base.method.__doc__
'another docstring'


So, if you want Base.foo and Derived.foo to share the same behavior but  
have separate docstrings (what Steven asked for), you have to create  
separate functions for both. That's what my already posted code does.

> So, are the demands of the methods reasonable, or do they already have
> what they are so loudly demanding?

Well, they could demand the right to have a docstring of their own. Since  
3.x eliminated a whole category of methods (unbound methods are gone),  
their ability to press for demands was greatly reduced. I don't think they  
could get such right granted in the near future...

-- 
Gabriel Genellina




More information about the Python-list mailing list