Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'help?': 0.03; 'sys': 0.05; 'attribute': 0.09; 'command-line': 0.09; 'subclass': 0.09; 'to:addr:python.list': 0.09; 'to:addr:tim.thechases.com': 0.09; 'to:name:tim chase': 0.09; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; '"test"': 0.16; '-tkc': 0.16; '@property': 0.16; '[1].': 0.16; 'docstring': 0.16; 'go?': 0.16; 'inherit,': 0.16; 'inherited': 0.16; 'lookup': 0.16; 'metaclasses': 0.16; 'occasionally': 0.16; 'properties.': 0.16; 'runtime.': 0.16; 'stdout': 0.16; "type's": 0.16; 'what?': 0.16; '\xa0def': 0.16; '\xa0print': 0.16; '\xa0you': 0.16; 'matching': 0.16; 'cc:addr :python-list': 0.17; 'header:In-Reply-To:1': 0.21; '(like': 0.21; 'cc:2**0': 0.22; 'picking': 0.23; 'skip:\xa0 40': 0.23; 'fri,': 0.23; 'code': 0.24; "doesn't": 0.25; 'object': 0.26; 'pass': 0.27; 'message-id:@mail.gmail.com': 0.28; 'helpful': 0.28; 'subject:how': 0.29; 'subject:?': 0.29; 'import': 0.29; 'around.': 0.29; 'class.': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.30; 'chase': 0.30; 'decorators': 0.30; 'received:209.85.215': 0.30; 'looks': 0.31; 'seem': 0.32; 'skip:\xa0 30': 0.32; 'asking': 0.33; 'things': 0.33; "i've": 0.33; 'however,': 0.34; '8bit%:3': 0.35; 'doc': 0.35; 'instances': 0.35; 'using': 0.35; 'test': 0.35; '8bit%:5': 0.36; '8bit%:8': 0.36; 'none': 0.37; 'similar': 0.37; 'received:google.com': 0.37; 'something': 0.37; 'received:209.85': 0.37; 'eric': 0.38; 'subject:: ': 0.38; 'some': 0.38; '8bit%:6': 0.39; 'sometimes': 0.39; 'should': 0.39; 'perhaps': 0.39; 'received:209': 0.39; 'help': 0.40; 'property': 0.64; 'below.': 0.65; '(after': 0.67; "'test'": 0.84; '01:22': 0.84; 'datetime': 0.84; 'snow': 0.91; 'inheritance,': 0.93 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=U7ggUe16EeysBd/iovO8y07lx+DoiQNdb/8DIrxxwcE=; b=luH6KS2WedkpBqPjwvWD43sSjexoJjt8Vo3w+j9mPgf48szO3bRSe5ULTjVnuAURuZ k8lslWhylTDLy+2SSGalGXWARgGdB+18VGikzPUZqYTXT+NRc4+oD6zXssm6UKisTZuF DbD+8RKJecep15dBEKVSWMVWmVQX7BuM2tsq0= 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=o5GncsvqKvL3O7zFZiGnt4bdgTySHXc5oMOnEznO5F9zy/1+HoiSsZGVpnMkrTciqQ vI/WlnGgHfx5Muem6yn3TZMPOgI6MPtAN3PEEGsi91/rsDpr88cGUgyi7EvroEofVDtp RKmwto/3Wmrfz396JjiYeGltrR8dpnz3Pa8pw= MIME-Version: 1.0 In-Reply-To: <4DF1FA8B.2020609@tim.thechases.com> References: <4DF1FA8B.2020609@tim.thechases.com> Date: Fri, 10 Jun 2011 10:55:24 -0600 Subject: Re: how to inherit docstrings? From: Eric Snow To: Tim Chase Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list 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: 99 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307724927 news.xs4all.nl 49178 [::ffff:82.94.164.166]:57873 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7383 On Fri, Jun 10, 2011 at 5:05 AM, Tim Chase wrote: > 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. =A0You 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 > > =A0class Foo: > =A0 =A0@property > =A0 =A0def __doc__(self): > =A0 =A0 =A0return dynamically_generated_string > =A0 =A0 =A0# perhaps using introspection > However, on the class the property is just a property object (a descriptor). If you want to have a dynamic doc on the class then you have to do it on a metaclass: class Meta(type): @property def __doc__(cls): if not hasattr(cls, "_mydoc"): cls._mydoc =3D None return cls._mydoc class X(metaclass=3DMeta): pass X.__doc__ # None X._mydoc # None X._mydoc =3D "test" X.__doc__ # 'test' X().__doc__ # None The only problem, as seen in the last line, is that the __doc__ on instances is not inherited on instances of the class. Object attribute lookup only looks to the type's __dict__ for inheritance, and not the types's type. However, that should not be that hard to work around. -eric > 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 __do= c__ > properties. =A0Test code below. > > -tkc > > > > from datetime import datetime > from sys import stdout > class Base(object): > =A0"Base docstring" > =A0@property > =A0def __doc__(self): > =A0 =A0return datetime.now().strftime('%c') > > class WithDoc(Base): > =A0"WithDoc docstring" > =A0pass > > class WithoutDoc(Base): pass > > base =3D Base() > has =3D WithDoc() > lacks =3D WithoutDoc() > > for test in ( > =A0"help(base)", # why not in help? > =A0"help(has)", # expected > =A0"help(lacks)", # why not in help? > =A0"help(Base)", > =A0"help(WithDoc)", # expected > =A0"help(WithoutDoc)", > =A0"stdout.write(repr(base.__doc__))", # works > =A0"stdout.write(repr(has.__doc__))", # expected > =A0"stdout.write(repr(lacks.__doc__))", # where'd it go? > =A0"stdout.write(repr(Base.__doc__))", # expected > =A0"stdout.write(repr(WithDoc.__doc__))", # expected > =A0"stdout.write(repr(WithoutDoc.__doc__))", # what? > =A0): > =A0print test > =A0eval(test) > =A0print >