Newsgroups: comp.lang.python
From: Maxim Khitrov <mkhit...@gmail.com>
Date: Thu, 16 Jul 2009 21:21:16 -0400
Local: Thurs, Jul 16 2009 9:21 pm
Subject: Re: Override a method but inherit the docstring
On Thu, Jul 16, 2009 at 9:13 PM, Jean-Paul Calderone<exar...@divmod.com> wrote: Another way is to use a metaclass. Have its __new__ method loop > On Fri, 17 Jul 2009 11:01:49 +1000, Ben Finney <ben+pyt...@benfinney.id.au> > wrote: >> Howdy all, >> The following is a common idiom:: >> class FooGonk(object): >> class BarGonk(FooGonk): >> The docstring for ‘FooGonk.frobnicate’ is, intentionally, perfectly >> Ideally there would be a way to specify that the docstring should be >> class BarGonk(FooGonk): >> but that violates DRY (the association between BarGonk and FooGonk is >> What is the most Pythonic, DRY-adherent, and preferably least-ugly > How about this? > class BarGonk(FooGonk): > The implementation of "inherit_docstring" is left as an exercise for the > By the way, I don't think this is a particularly good idea. Presumably > class BarGonk(FooGonk): > With the result of BarGonk.frobnicate.__doc__ being set to: > Frobnicate this gonk. > This implementation takes the warble into consideration. through all attributes and compare those with what is already defined in bases. If you find a match, copy the __doc__ attribute. The advantage here is that it will work for all methods without any additional code, not counting the "__metaclass__ = ..." line. If you define a metaclass for the base, then no modifications are required for any subclasses. I do agree, however, that the best thing to do is to write a very short explanation for what the override is for. - Max You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||