Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7396

Re: how to inherit docstrings?

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 <ericsnowcurrently@gmail.com>
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 <BANLkTim9edL6pDEgQohGfKn8j3q-NKu3Rw@mail.gmail.com>
References <BANLkTin3FGoxwvH2VDOb4OhAWi2Ea3K-pA@mail.gmail.com> <4DF1FA8B.2020609@tim.thechases.com> <BANLkTikqr1s0OVV15Pi4Gz4KPJxtHVASfA@mail.gmail.com> <BANLkTim9edL6pDEgQohGfKn8j3q-NKu3Rw@mail.gmail.com>
Date Fri, 10 Jun 2011 12:58:26 -0600
Subject Re: how to inherit docstrings?
From Eric Snow <ericsnowcurrently@gmail.com>
To Ian Kelly <ian.g.kelly@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list <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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.95.1307732308.11593.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Fri, Jun 10, 2011 at 11:26 AM, Ian Kelly <ian.g.kelly@gmail.com> 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):
> ...   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__ = DocDescriptor()
> ...
>>>> class X(object):
> ...   __metaclass__ = Meta
> ...
>>>> X.__doc__
>>>> X().__doc__
>>>> X._mydoc = '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
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: how to inherit docstrings? Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-10 12:58 -0600

csiph-web