Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!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; 'attribute': 0.09; 'descriptor': 0.09; 'dynamically': 0.09; 'none)': 0.09; '>>>': 0.12; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'bases,': 0.16; 'descriptors.': 0.16; 'inherited': 0.16; 'lookup': 0.16; 'metaclass': 0.16; "type's": 0.16; 'cc:addr :python-list': 0.17; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'focuses': 0.23; 'fri,': 0.23; 'received:209.85.161.46': 0.23; 'received:mail-fx0-f46.google.com': 0.23; 'received:209.85.161': 0.26; 'string': 0.26; 'pass': 0.27; 'message-id:@mail.gmail.com': 0.28; 'subject:how': 0.29; 'subject:?': 0.29; 'around.': 0.29; 'class.': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.30; 'looks': 0.31; 'adds': 0.32; 'there': 0.35; 'doc': 0.35; 'instances': 0.35; 'cc:2**1': 0.35; 'properties': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'eric': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'received:209': 0.39; 'either': 0.39; 'easily': 0.60; 'generate': 0.60; 'your': 0.60; "'test'": 0.84; '10:55': 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:from:date :message-id:subject:to:cc:content-type:content-transfer-encoding; bh=LIz0iAQHvExRA4f1MT7uTQE06xb4wbWFD+DDfRjeR9k=; b=tmzCwwf0lZGvgdHOSrwHUHuWGiCwFa+GxFyJ5URMwI9hNP0fZhTQygSaVUtdZHrs8X 5RcoXutRALefYZwP/4r4PQrGaLSh4Jcg4Pt2ckLTfmQ5BHmLI0QH7shILoP/+MhMeCvS 0flaqCsxcW5b5Tq4+3fCZTimlQOnIZqiZc9Rc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=TGREPMmDRibs2uVrKbz2oXi3DKL28O+gSEecboLF6uCJ6W6uhEujUd+bfbItsWPL+M vtp639d4oZPS7tKuL/0SHwy4iffllx80nJyzyZeS8HyxbmQZaaTGVsgGj1qO/sEPliKf xadSFW0p6HlyrvJr4GL6CIrMoPR/dBynNoI2o= MIME-Version: 1.0 In-Reply-To: References: <4DF1FA8B.2020609@tim.thechases.com> From: Ian Kelly Date: Fri, 10 Jun 2011 11:26:29 -0600 Subject: Re: how to inherit docstrings? To: Eric Snow 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: 45 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307726821 news.xs4all.nl 49045 [::ffff:82.94.164.166]:60439 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7387 On Fri, Jun 10, 2011 at 10:55 AM, Eric Snow w= rote: > The only problem, as seen in the last line, is that the __doc__ on > instances is not inherited on instances of the class. =A0Object > attribute lookup only looks to the type's __dict__ for inheritance, > and not the types's type. =A0However, that should not be that hard to > work around. Everybody always focuses so much on properties and forgets that you can also just write your own descriptors. >>> class DocDescriptor(object): ... def __get__(self, instance, owner): ... return getattr(owner, "_mydoc", None) ... >>> class Meta(type): ... def __init__(cls, name, bases, d): ... super(Meta, cls).__init__(name, bases, d) ... cls.__doc__ =3D DocDescriptor() ... >>> class X(object): ... __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