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


Groups > comp.lang.python > #7385

Re: how to inherit docstrings?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'string,': 0.05; 'python': 0.08; 'assert': 0.09; 'object.': 0.09; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'bases,': 0.16; 'calculating': 0.16; 'docstring': 0.16; 'inherited': 0.16; 'metaclass': 0.16; 'metaclasses': 0.16; 'mro': 0.16; 'non-empty': 0.16; '\xa0for': 0.16; 'cc:addr:python- list': 0.17; '(which': 0.20; 'header:In-Reply-To:1': 0.21; 'skip:m 30': 0.21; "aren't": 0.22; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'example.': 0.23; '\xa0if': 0.23; 'fri,': 0.23; 'code': 0.24; 'function': 0.25; 'url:mailman': 0.26; 'object': 0.26; 'message-id:@mail.gmail.com': 0.28; 'subject:how': 0.29; 'subject:?': 0.29; 'opposed': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.30; 'url:listinfo': 0.30; 'decorators': 0.30; 'received:209.85.215': 0.30; 'it.': 0.31; 'steven': 0.32; "d'aprano": 0.35; 'skip:{ 10': 0.35; 'trigger': 0.35; 'idea': 0.36; 'uses': 0.36; 'none': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'some': 0.38; '8bit%:6': 0.39; 'received:209': 0.39; 'empty': 0.39; 'really': 0.40; 'factory': 0.73; 'want,': 0.74; 'exclude': 0.77; '10:47': 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=YhnBa9+yDKXcSNX3Cccw4F09s2x5nwUjlsjsOXTbDdA=; b=fJJQSBCfBQgj5BY37h/WOLumMR3ulGm6dCFgQABYAufxDmJV9CqAANbj0uwPko1XgX jvdmpaus9tAe6Utq7Y50RSgZ7cq054Vmd0hbupIR30k+6k56mfgSfvNgX9su6ij0DoB2 n88q8zw22xuofgL+Kkw1i03ecVIkS9Sl9cwYw=
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=iMXRDZlulSZ9wOWd9K+24gf6RBuG38O2ws1lTzYW0fJBY0uduQh2tz+J/t1pXZqhCV g/W8TlnfJOWczWg3ym63zmSLBNAqK8Sg/E3fZvgOBQCOQyurnLQFs9aNhV2e7P2i9la9 76YtWMMaVmrgDJLY6jpNvaqF/NXZeD2VxfVpU=
MIME-Version 1.0
In-Reply-To <4df24a87$0$30002$c3e8da3$5496439d@news.astraweb.com>
References <mailman.42.1307600576.11593.python-list@python.org> <4df24a87$0$30002$c3e8da3$5496439d@news.astraweb.com>
Date Fri, 10 Jun 2011 11:01:41 -0600
Subject Re: how to inherit docstrings?
From Eric Snow <ericsnowcurrently@gmail.com>
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
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.86.1307725303.11593.python-list@python.org> (permalink)
Lines 72
NNTP-Posting-Host 82.94.164.166
X-Trace 1307725303 news.xs4all.nl 49179 [::ffff:82.94.164.166]:53766
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7385

Show key headers only | View raw


On Fri, Jun 10, 2011 at 10:47 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Here's some Python 3 code that uses a factory function as a metaclass to
> inherit docstrings. Give the class a docstring of an empty string, and it
> will be inherited from the first superclass found with a non-empty
> docstring.
>
>

Yeah, the idea of an empty docstring to trigger docstring inheritance
really appeals to me.  Nice example.  Incidently, aren't metaclasses
always inherited, as opposed to class decorators (which are never)?

-eric

>
> def InheritableDocstring(name, bases, dict):
>    mro = None
>    docstring = dict.get('__doc__')
>    if docstring == '':
>        # Search the MRO for the first non-empty docstring. We let Python
>        # do all the hard work of calculating the MRO.
>        mro = type('K', bases, {}).__mro__[1:]  # Exclude the class K.
>        # Also exclude object.
>        assert mro[-1] == object
>        mro = mro[:-1]
>        for cls in mro:
>            if cls.__doc__:
>                docstring = cls.__doc__
>                break
>        else:
>            docstring = None
>        dict['__doc__'] = docstring
>    assert dict.get('__doc__') != ''
>    # Create the class we want, and return it.
>    cls = type(name, bases, dict)
>    if mro:
>        assert cls.__mro__ == (cls,) + mro + (object,)
>    return cls
>
>
>
> class A(metaclass=InheritableDocstring):
>    pass
>
> class B(A, metaclass=InheritableDocstring):
>    ''
>
> class C(B, metaclass=InheritableDocstring):
>    'A docstring.'
>
> class D(B, metaclass=InheritableDocstring):
>    pass
>
> class E(D, C, metaclass=InheritableDocstring):
>    ''
>
> class F(E, metaclass=InheritableDocstring):
>    ''
>
> assert all(cls.__doc__ is None for cls in (A, B, D))
> assert all(cls.__doc__ == 'A docstring.' for cls in (C, E, F))
>
>
>
> --
> Steven
> --
> 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

how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-09 00:22 -0600
  Re: how to inherit docstrings? Ben Finney <ben+python@benfinney.id.au> - 2011-06-09 16:37 +1000
    Re: how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-09 01:13 -0600
      Re: how to inherit docstrings? Ben Finney <ben+python@benfinney.id.au> - 2011-06-09 17:44 +1000
        Re: how to inherit docstrings? Duncan Booth <duncan.booth@invalid.invalid> - 2011-06-09 11:23 +0000
        Re: how to inherit docstrings? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-09 14:56 +0000
          Re: how to inherit docstrings? Ben Finney <ben+python@benfinney.id.au> - 2011-06-10 07:33 +1000
            Re: how to inherit docstrings? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-10 09:25 +0000
  Re: how to inherit docstrings? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-10 16:47 +0000
    Re: how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-10 11:01 -0600
      Re: how to inherit docstrings? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-10 17:27 +0000
        Re: how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-10 12:48 -0600
    Re: how to inherit docstrings? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-10 17:19 +0000

csiph-web