Inheriting methods but over-riding docstrings

Steve Holden steve at holdenweb.com
Sun Jan 17 21:23:45 EST 2010


Gabriel Genellina wrote:
> En Sat, 16 Jan 2010 14:55:11 -0300, Steven D'Aprano
> <steve at remove-this-cybersource.com.au> escribió:
> 
>> I have a series of subclasses that inherit methods from a base class, but
>> I'd like them to have their own individual docstrings. The obvious
>> solution (other than copy-and-paste) is this:
>>
>>
>> class Base(object):
>>     colour = "Blue"
>>     def parrot(self):
>>         """docstring for Base"""
>>         return "Norwegian %s" % self.colour
>>
>>
>> class SubClass(Base):
>>     colour = "Red"
>>     def parrot(self):
>>         """docstring for Subclass"""
>>         return super(Subclass, self).parrot()
>>
>>
>> but that adds an awful lot of boilerplate to my subclasses. Are there any
>> other good solutions to this problem?
> 
> 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:

>>> class TheHellOfIt:
...     """Classes, of course, as first-class citizens and have
docstrings."""
...     def method(self):
...         """Discrimination against methods, I call it."""
...         pass
...
>>> print TheHellOfIt.method.__doc__
Discrimination against methods, I call it.
>>> import sys
>>> help(sys.modules["__main__"])

Gives:

Help on built-in module __main__:

NAME
    __main__

FILE
    (built-in)

CLASSES
    TheHellOfIt

    class TheHellOfIt
     |  Classes, of course, as first-class citizens and have docstrings.
     |
     |  Methods defined here:
     |
     |  method(self)
     |      Discrimination against methods, I call it.

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

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list