[SciPy-Dev] Reusing docstrings of the parent class in overridden methods of a subclass
Warren Weckesser
warren.weckesser at gmail.com
Thu May 23 18:32:22 EDT 2013
On Thu, May 23, 2013 at 5:46 PM, Warren Weckesser <
warren.weckesser at gmail.com> wrote:
>
> On Thu, May 23, 2013 at 4:56 PM, Warren Weckesser <
> warren.weckesser at gmail.com> wrote:
>
>> In scipy.stats.distributions, some of the distribution classes
>> (subclasses of rv_continuous) override, say, the `fit` method. See, for
>> example, gamma_gen or beta_gen. Is there a standard way for the `fit`
>> method of the subclass to use the docstring of the `fit` method in the
>> parent class? In python 2.7, this monkey patching seems to work:
>>
>> beta_gen.fit.__func__.__doc__ = rv_continuous.fit.__doc__
>>
>> (This is what I did in https://github.com/scipy/scipy/pull/2519)
>>
>> In python 3.3, however, beta_gen.fit does not have the __func__ attribute.
>>
>> Is there a recommended way to do this? The docstring is rather long, and
>> I'd rather not repeat it in the subclass.
>>
>>
>
> This decorator:
>
> def add_doc(value):
> def _doc(func):
> func.__doc__ = value
> return func
> return _doc
>
> from the last answer here:
>
> http://stackoverflow.com/questions/4056983/how-do-i-programmatically-set-the-docstring
> works in python 2.7 and 3.3. It looks like this in use:
>
> @add_doc(rv_continuous.fit.__doc__)
> def fit(self, data, *args, **kwds):
> ...
>
> Anyone have an alternative that would be preferred?
>
>
This is the variation of the decorator that I'll use:
def inherit_docstring_from(cls):
"""
This decorator copies the docstring from `cls`s version of
the decorated method to the decorated method.
"""
def _doc(func):
func.__doc__ = getattr(cls, func.__name__).__doc__
return func
return _doc
Warren
> Warren
>
>
>
>> Warren
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20130523/2b4faf8d/attachment.html>
More information about the SciPy-Dev
mailing list