Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'instance': 0.05; 'instance,': 0.05; 'modified': 0.05; 'desired.': 0.07; '>>>>': 0.09; 'descriptor': 0.09; 'dynamically': 0.09; 'instance.': 0.09; 'kelly': 0.09; 'none)': 0.09; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'bases,': 0.16; 'descriptors.': 0.16; 'inherited': 0.16; 'metaclass': 0.16; 'cc:addr:python-list': 0.17; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'focuses': 0.23; 'fri,': 0.23; 'pointed': 0.25; 'string': 0.26; 'pass': 0.27; "i'm": 0.27; 'message-id:@mail.gmail.com': 0.28; 'subject:how': 0.29; 'subject:?': 0.29; 'forgot': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.30; 'received:209.85.215': 0.30; 'adds': 0.32; '...': 0.34; 'there': 0.35; 'doc': 0.35; 'cc:2**1': 0.35; 'properties': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'subject:: ': 0.38; 'called': 0.39; 'received:209': 0.39; 'returned': 0.39; 'either': 0.39; 'totally': 0.40; 'custom': 0.60; 'easily': 0.60; 'generate': 0.60; 'simply': 0.60; 'your': 0.60; 'glad': 0.66; "'test'": 0.84; '11:26': 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=aP1oQFAEONgH1JzYIqv5vMHG14woLZ/OGSbrpCEwE8E=; b=FZlEPuTYSmGj8H0HJFIiKricd6TMTL4W7LA0+ammGCGOjo95ADD2pCC+UGkOszmwEs EIEXbMeuOzRomanIiLBCETtN0TL+0Z/V8T9Ac2e9PlShk/i4DRN4JBg4gckFk9dz78eZ kxuCOr9x+5Py7OvWm7Xij/nTxdQkIgobEDWfQ= 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=ADgOscs11edQPDUE/crOhZ3W1PlNr6YrC2vsqnFhjXgKgJbx1lLPdFvV/DcRqtruaH PSEKg1hkP3g2d0ns0u9zwDldg22uCGOrLd2mY0aFA2Y+agdD/W7RKbgAGiYE2/Y3uuce J0BVECffv1quh3VjJulZaI1fPZ6DHSQeCi9Vg= MIME-Version: 1.0 In-Reply-To: References: <4DF1FA8B.2020609@tim.thechases.com> Date: Fri, 10 Jun 2011 12:58:26 -0600 Subject: Re: how to inherit docstrings? From: Eric Snow To: Ian Kelly 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: 46 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307732308 news.xs4all.nl 49042 [::ffff:82.94.164.166]:53010 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7396 On Fri, Jun 10, 2011 at 11:26 AM, Ian Kelly wrote: > Everybody always focuses so much on properties and forgets that you > can also just write your own descriptors. > I'm so glad that you pointed this out. I totally forgot that properties simply returned themselves if not called on the instance. You are right about a custom descriptor. -eric >>>> class DocDescriptor(object): > ... =A0 def __get__(self, instance, owner): > ... =A0 =A0 return getattr(owner, "_mydoc", None) > ... >>>> class Meta(type): > ... =A0 def __init__(cls, name, bases, d): > ... =A0 =A0 super(Meta, cls).__init__(name, bases, d) > ... =A0 =A0 cls.__doc__ =3D DocDescriptor() > ... >>>> class X(object): > ... =A0 __metaclass__ =3D Meta > ... >>>> X.__doc__ >>>> X().__doc__ >>>> X._mydoc =3D 'test' >>>> X.__doc__ > 'test' >>>> X().__doc__ > 'test' >>>> class Derived(X): pass > ... >>>> Derived.__doc__ > 'test' >>>> Derived().__doc__ > 'test' > > There you go, a metaclass that adds a __doc__ descriptor that can be > inherited (thanks to the metaclass), can be accessed from either the > class or the instance (thanks to the descriptor), and can easily be > modified to generate the doc string dynamically at call-time if > desired. > > Cheers, > Ian >