Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!newsfeed.kpn.net!pfeed08.wxs.nl!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; 'context': 0.04; 'everyone!': 0.05; 'string,': 0.05; 'default,': 0.07; 'decorator': 0.09; 'default)': 0.09; 'default:': 0.09; 'func': 0.09; 'namespace': 0.09; 'none)': 0.09; 'none:': 0.09; 'subclass': 0.09; 'url:activestate': 0.09; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; '12:22': 0.16; '[1].': 0.16; 'docstring': 0.16; 'docstrings': 0.16; 'false)': 0.16; 'fname': 0.16; 'inheriting': 0.16; 'metaclass': 0.16; 'metaclasses': 0.16; 'obj,': 0.16; 'trigger.': 0.16; '\xa0you': 0.16; 'class,': 0.16; 'matching': 0.16; 'thanks!': 0.16; 'url:code': 0.17; 'to:name:python-list': 0.19; 'header:In-Reply-To:1': 0.21; 'thu,': 0.22; 'objects': 0.23; 'function': 0.25; 'message-id:@mail.gmail.com': 0.28; 'thanks': 0.28; 'subject:how': 0.29; 'subject:?': 0.29; 'bound': 0.29; 'class.': 0.29; 'class': 0.29; 'instead': 0.29; 'decorators': 0.30; 'email name:': 0.30; 'none,': 0.30; 'received:209.85.215': 0.30; 'received:209.85.215.46': 0.30; 'received:mail- ew0-f46.google.com': 0.30; 'changes': 0.30; 'does': 0.33; 'to:addr :python-list': 0.33; '...': 0.34; 'thinking': 0.34; 'however,': 0.34; '[1]': 0.34; 'there': 0.35; 'doc': 0.35; 'skip:@ 10': 0.35; 'trigger': 0.35; 'using': 0.35; 'idea': 0.36; 'none': 0.37; 'feedback': 0.37; 'too.': 0.37; 'received:google.com': 0.37; 'something': 0.37; 'change': 0.37; 'received:209.85': 0.37; 'thread': 0.37; 'pretty': 0.37; 'url:python': 0.38; 'hoping': 0.38; 'url:org': 0.38; 'but': 0.38; 'eric': 0.38; 'realize': 0.38; 'subject:: ': 0.38; 'sometimes': 0.39; 'received:209': 0.39; 'empty': 0.39; 'got': 0.39; 'to:addr:python.org': 0.39; 'missing': 0.40; 'really': 0.40; 'p.s.': 0.67; '(after': 0.67; 'doing.': 0.73; '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:content-type:content-transfer-encoding; bh=2VgBynrOiINbdgrjX9FM0NJUcDQPZ1AQ1aLxhAdgFt0=; b=Gw/0ml1UBzMOdSLDORQbf/vTkc3wkaaLvI6eBD5t6KzPYe+ck9DswmRkbT+17qEmsa rG5DSDeWOdO/SGH+dvjajZl7FlMMDx7PRkAm59u7lljceEfDmjxI3HiaXqglz7RaLslW ixN6BvtMfD5PdX1xQL16pLO0WWDH2dGN6NQII= 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 :content-type:content-transfer-encoding; b=D36nQzUC5QJ1CFSTeEBEHRJrAkG5LeWYEeCEQycjDs1PjpHRF+rk7iBvDWjgSycBjc UjEDJlTaVOI3ZB0dwoP8655m9jBfbJdOwMVBin7RDQ5tnzGZ0s9HNt4QrxG1cDkD7B3h 13dZduRvXwAEa/jEUkTz01heFlKVvfMyIVFdE= MIME-Version: 1.0 In-Reply-To: References: Date: Fri, 10 Jun 2011 17:25:28 -0600 Subject: Re: how to inherit docstrings? From: Eric Snow To: python-list Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 90 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307748335 news.xs4all.nl 49179 [::ffff:82.94.164.166]:52489 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7419 On Thu, Jun 9, 2011 at 12:22 AM, Eric Snow wr= ote: > 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]. > > However, I was hoping for a way to do it with just function decorators > on the methods (no metaclass or class decorator). =A0I am not sure if > this is doable. =A0I realize now that this is exactly the reason I got > to thinking last week about objects being notified when they are bound > [2]. > > So, is there a way to do this with just decorators, or am I "stuck" > with the metaclass/class decorator route? =A0(It's not all that bad :) > Thanks for all the feedback on this thread everyone! I found a solution that works pretty well, using descriptors: class DocFunc: TRIGGER =3D None def __init__(self, f): self.f =3D f def __get__(self, obj, cls): doc =3D self.f.__doc__ if doc =3D=3D self.TRIGGER: doc =3D self.get_doc(cls, self.f.__name__, self.TRIGGER) self.f.__doc__ =3D doc setattr(cls, self.f.__name__, self.f) return self.f @staticmethod def get_doc(cls, fname, default=3DTRIGGER, member=3DTrue): bases =3D cls.__mro__[:] if member: bases =3D bases[1:] for base in bases: func =3D getattr(base, fname, None) if not func: continue doc =3D getattr(func, '__doc__', default) if doc =3D=3D default: continue return doc return default @staticmethod def inherits_docstring(f, context=3DNone, fname=3DNone, default=3DT= RIGGER): if context is not None: cls, namespace =3D context fname =3D fname or f.__name__ f.__doc__ =3D DocFunc.get_doc(cls, fname, default, False) return f return DocFunc(f) class X: def something(self): """some method""" class Y(X): @DocFunc.inherits_docstring def something(self): ... This approach does not update the docstring if it changes in the base class, but I don't need that for what I am doing. If you want to trigger on an empty string, instead of None, just change the TRIGGER. -eric > Thanks! > > -eric > > > p.s. Am I missing something or can you really not change the docstring > of a class? =A0I was thinking about the idea of inheriting class > docstrings too. > > > [1] http://code.activestate.com/recipes/577743-using-decorators-to-inheri= t-function-docstrings/ > [2] http://mail.python.org/pipermail/python-ideas/2011-June/010446.html >