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: 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: <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 To: "Steven D'Aprano" 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Fri, Jun 10, 2011 at 10:47 AM, Steven D'Aprano 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): > =A0 =A0mro =3D None > =A0 =A0docstring =3D dict.get('__doc__') > =A0 =A0if docstring =3D=3D '': > =A0 =A0 =A0 =A0# Search the MRO for the first non-empty docstring. We let= Python > =A0 =A0 =A0 =A0# do all the hard work of calculating the MRO. > =A0 =A0 =A0 =A0mro =3D type('K', bases, {}).__mro__[1:] =A0# Exclude the = class K. > =A0 =A0 =A0 =A0# Also exclude object. > =A0 =A0 =A0 =A0assert mro[-1] =3D=3D object > =A0 =A0 =A0 =A0mro =3D mro[:-1] > =A0 =A0 =A0 =A0for cls in mro: > =A0 =A0 =A0 =A0 =A0 =A0if cls.__doc__: > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0docstring =3D cls.__doc__ > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0break > =A0 =A0 =A0 =A0else: > =A0 =A0 =A0 =A0 =A0 =A0docstring =3D None > =A0 =A0 =A0 =A0dict['__doc__'] =3D docstring > =A0 =A0assert dict.get('__doc__') !=3D '' > =A0 =A0# Create the class we want, and return it. > =A0 =A0cls =3D type(name, bases, dict) > =A0 =A0if mro: > =A0 =A0 =A0 =A0assert cls.__mro__ =3D=3D (cls,) + mro + (object,) > =A0 =A0return cls > > > > class A(metaclass=3DInheritableDocstring): > =A0 =A0pass > > class B(A, metaclass=3DInheritableDocstring): > =A0 =A0'' > > class C(B, metaclass=3DInheritableDocstring): > =A0 =A0'A docstring.' > > class D(B, metaclass=3DInheritableDocstring): > =A0 =A0pass > > class E(D, C, metaclass=3DInheritableDocstring): > =A0 =A0'' > > class F(E, metaclass=3DInheritableDocstring): > =A0 =A0'' > > assert all(cls.__doc__ is None for cls in (A, B, D)) > assert all(cls.__doc__ =3D=3D 'A docstring.' for cls in (C, E, F)) > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list >