Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7374

Re: how to inherit docstrings?

Date 2011-06-10 06:05 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: how to inherit docstrings?
References <BANLkTin3FGoxwvH2VDOb4OhAWi2Ea3K-pA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.81.1307703964.11593.python-list@python.org> (permalink)

Show all headers | View raw


On 06/09/2011 01:22 AM, Eric Snow wrote:
> Sometimes when using class inheritance, I want the overriding methods
> of the subclass to get the docstring of the matching method in the
> base class.  You can do this with decorators (after the class
> definition), with class decorators, and with metaclasses [1].

While asking for __doc__ ponies and picking colors for 
bike-sheds, in a similar vein, I've occasionally wanted to do 
something like

   class Foo:
     @property
     def __doc__(self):
       return dynamically_generated_string
       # perhaps using introspection

This would have been most helpful in things like generating help 
(like in command-line parsers), where the doc-string can 
introspect the class and learn about its methods at runtime. 
Some things seem to inherit, some don't, and help() doesn't seem 
to pick up on any of the dynamically-defined __doc__ properties. 
  Test code below.

-tkc



from datetime import datetime
from sys import stdout
class Base(object):
   "Base docstring"
   @property
   def __doc__(self):
     return datetime.now().strftime('%c')

class WithDoc(Base):
   "WithDoc docstring"
   pass

class WithoutDoc(Base): pass

base = Base()
has = WithDoc()
lacks = WithoutDoc()

for test in (
   "help(base)", # why not in help?
   "help(has)", # expected
   "help(lacks)", # why not in help?
   "help(Base)",
   "help(WithDoc)", # expected
   "help(WithoutDoc)",
   "stdout.write(repr(base.__doc__))", # works
   "stdout.write(repr(has.__doc__))", # expected
   "stdout.write(repr(lacks.__doc__))", # where'd it go?
   "stdout.write(repr(Base.__doc__))", # expected
   "stdout.write(repr(WithDoc.__doc__))", # expected
   "stdout.write(repr(WithoutDoc.__doc__))", # what?
   ):
   print test
   eval(test)
   print

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: how to inherit docstrings? Tim Chase <python.list@tim.thechases.com> - 2011-06-10 06:05 -0500

csiph-web