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


Groups > comp.lang.python > #7341

Re: how to inherit docstrings?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ericsnowcurrently@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'own.': 0.03; 'implicit': 0.07; 'override': 0.07; "shouldn't": 0.07; 'used.': 0.07; 'called.': 0.09; 'decorator': 0.09; 'none:': 0.09; 'subclass': 0.09; 'pm,': 0.10; 'this:': 0.10; 'def': 0.12; 'wrote:': 0.14; '(plain': 0.16; 'bases,': 0.16; 'docstring': 0.16; 'docstrings': 0.16; 'hierarchy': 0.16; 'imo,': 0.16; 'inherited': 0.16; 'inheriting': 0.16; 'metaclass': 0.16; 'originally,': 0.16; 'presumably,': 0.16; 'somehow,': 0.16; 'to:addr:comp.lang.python': 0.16; 'class,': 0.16; 'question.': 0.16; 'cc:addr:python-list': 0.17; 'language': 0.18; 'header:In-Reply-To:1': 0.21; 'seems': 0.21; 'thu,': 0.22; 'cc:2**0': 0.22; 'stuff': 0.22; 'cc:no real name:2**0': 0.23; 'gregory': 0.23; 'smart': 0.23; '\xa0so': 0.23; 'wonder': 0.23; 'skip:b 20': 0.23; "doesn't": 0.25; 'there.': 0.25; 'function': 0.25; 'expect': 0.25; 'url:mailman': 0.26; 'classes': 0.26; 'definition': 0.26; 'not.': 0.26; 'object': 0.26; "i'm": 0.27; 'message-id:@mail.gmail.com': 0.28; 'changing': 0.28; 'subject:how': 0.29; 'subject:?': 0.29; 'explicitly': 0.29; 'class': 0.29; 'implement': 0.30; "won't": 0.30; 'cc:addr:python.org': 0.30; 'url:listinfo': 0.30; 'carl': 0.30; 'ewing': 0.30; 'received:209.85.215': 0.30; 'changes': 0.30; 'sort': 0.31; 'usually': 0.32; 'capture': 0.32; 'cases': 0.32; 'execution': 0.32; 'does': 0.33; 'actually': 0.33; 'it?': 0.33; 'too': 0.33; 'rather': 0.34; 'however,': 0.34; 'force': 0.34; 'that,': 0.34; 'explicit': 0.35; 'frame': 0.35; 'help,': 0.36; 'uses': 0.36; 'created': 0.36; 'too.': 0.37; 'received:google.com': 0.37; 'something': 0.37; 'change': 0.37; 'received:209.85': 0.37; 'thursday,': 0.37; 'put': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'should': 0.39; 'called': 0.39; 'said': 0.39; 'perhaps': 0.39; "i'd": 0.39; 'received:209': 0.39; 'either': 0.39; 'help': 0.40; 'happen': 0.60; 'worth': 0.60; 'easily': 0.60; 'more': 0.60; 'body': 0.61; 'back': 0.63; 'to:addr:googlegroups.com': 0.63; 'direct': 0.65; 'here': 0.66; 'special': 0.66; 'ways,': 0.67; 'june': 0.77; 'avoid.': 0.84; 'locals': 0.84; 'nice,': 0.84; 'risky': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=lvpvXFjAeDUqONaHF9INx3BxCe8T8Pp0i9J12vkh07Q=; b=Z4+3D15TskULsuNqYXdkJ7W0EZ/3NNcoPYhvj27fJpgfkz1ycVOMLKoSjkrs+GyvDB Ky6VjU0Ff4O8GU5/uHjihOsFgyCTQ3fXOs3mifv0EEy2fqJHJgQi54Kmxojt6Vtz+LWt SDB6TWRNeQImATGqotIOrhBq88FpIJubG+xT4=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=rvNrdhqoLt9aWBrgAbZOi4i1ZkUcg/mZzI5Pgvgg/lQFB9dD6ffyr9yiiQ7iIkn65c V8s7CSIRqvgR+k9Y2NK6rlaU7yAAFPgQ8tK6oWfvP3LUQ2mBD+4Ux4NLBMf9JWAF0ZVN nUcFltobHoXjW66IkNrqUwy596q2ily79EOKk=
MIME-Version 1.0
In-Reply-To <b5497fa4-b79b-454e-b2e1-9e48642e2d85@glegroupsg2000goo.googlegroups.com>
References <95cvmqF5b0U1@mid.individual.net> <b5497fa4-b79b-454e-b2e1-9e48642e2d85@glegroupsg2000goo.googlegroups.com>
Date Thu, 9 Jun 2011 20:37:19 -0600
Subject Re: how to inherit docstrings?
From Eric Snow <ericsnowcurrently@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.71.1307673443.11593.python-list@python.org> (permalink)
Lines 72
NNTP-Posting-Host 82.94.164.166
X-Trace 1307673443 news.xs4all.nl 49039 [::ffff:82.94.164.166]:46422
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7341

Show key headers only | View raw


On Thu, Jun 9, 2011 at 7:12 PM, Carl Banks <pavlovevidence@gmail.com> wrote:
> On Thursday, June 9, 2011 3:27:36 PM UTC-7, Gregory Ewing wrote:
>> IMO, it shouldn't be necessary to explicitly copy docstrings
>> around like this in the first place. Either it should happen
>> automatically, or help() should be smart enough to look up
>> the inheritance hierarchy when given a method that doesn't
>> have a docstring of its own.
>
> Presumably, the reason you are overriding a method in a subclass is to change its behavior; I'd expect an inherited docstring to be inaccurate more often than not.  So I'd be -1 on automatically inheriting them.
>

When I write ABCs to capture an interface, I usually put the
documentation in the docstrings there.  Then when I implement I want
to inherit the docstrings.  Implicit docstring inheritance for
abstract base classes would meet my needs.  I'm just not clear on the
impact this would have for the other use cases of docstrings.

> However, I'd be +1 easily on a little help from the language to explicitly request to inherit the docstring.
>

Yeah, that's more or less how I feel too.  But what would fill that
role?  This comes back to my original question.  A method at
definition time does not know its class, nor would the decorator, so
they won't know where from to inherit the docstring.

Like I said originally, you can approach this a number of ways, but
the one that appeals to me most (plain function decorators) doesn't
work without some explicit help, which I would rather avoid.  Implicit
help would be nice, but how to do it?

The most direct form, presenting the class to the execution frame of
the body somehow, seems risky and strange.  It's sort of like the
function object being inserted into the locals when it is called.
However, the class object would have to be created before the body
gets exec'ed, rather than as now, where <metaclass>.__new__ is called
after...  Changing that would require changes to type.__new__ and how
it's used.

Perhaps a good approach would be to have a special decorator in the
stdlib that type.__new__ would recognize, like this:

def inherits_docstring(f):
    if f.__doc__ is None:
        f.__doc__ = NotImplemented
    return f

# actually in typeobject.c, or something
def type.__new__(meta, name, bases, namespace):
    # do the normal stuff here
    # now handle docstring inheritance
    for name, obj in namespace.items():
        if hasattr(obj, "__doc__") and obj.__doc__ is NotImplemented:
            # inherit the docstring...

But then I look at that and wonder if it's too special-cased to be
worth the trouble.  I can just use a metaclass or class decorator that
does that, and override builtin.__build__class__ to force its use
everywhere; or use one base class for all my classes that uses the
metaclass.  But it would be nice to have implicit support.

-eric


>
> Carl Banks
> --
> http://mail.python.org/mailman/listinfo/python-list
>

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


Thread

Re: how to inherit docstrings? Carl Banks <pavlovevidence@gmail.com> - 2011-06-09 18:12 -0700
  Re: how to inherit docstrings? Ben Finney <ben+python@benfinney.id.au> - 2011-06-10 11:42 +1000
  Re: how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-09 20:37 -0600
  Re: how to inherit docstrings? Terry Reedy <tjreedy@udel.edu> - 2011-06-09 23:59 -0400
    Re: how to inherit docstrings? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-10 09:48 +0000
  Re: how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-09 22:41 -0600
  Re: how to inherit docstrings? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-06-10 19:16 +1200

csiph-web