Newsgroups: comp.lang.python
From: Shai <s...@platonix.com>
Date: Thu, 23 Jul 2009 16:51:43 -0700 (PDT)
Local: Thurs, Jul 23 2009 7:51 pm
Subject: Re: Override a method but inherit the docstring
On Jul 17, 10:52 am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote: ... which points to the better solution: use a descriptor. With the > When the decorator is called, the function object is just a function doc_inherit decorator defined below, one may write class Foo(object): class Bar(Foo): and it appears to work. The code below is a little longish because we from functools import wraps class DocInherit(object): The class itself is also used as a decorator def __init__(self, mthd): def __get__(self, obj, cls): def get_with_inst(self, obj, cls): overridden = getattr(super(cls, obj), self.name, None) @wraps(self.mthd, assigned=('__name__','__module__')) return self.use_parent_doc(f, overridden) def get_no_inst(self, cls): for parent in cls.__mro__[1:]: @wraps(self.mthd, assigned=('__name__','__module__')) return self.use_parent_doc(f, overridden) def use_parent_doc(self, func, source): doc_inherit = DocInherit Combining docstrings (as suggested by Jean-Paul Calderone), and Have fun, You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||