Reusing docstrings of the parent class in overridden methods of a subclass

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. Warren

On Thu, May 23, 2013 at 4:56 PM, Warren Weckesser < warren.weckesser@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... 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? Warren
Warren

On Thu, May 23, 2013 at 5:46 PM, Warren Weckesser < warren.weckesser@gmail.com> wrote:
On Thu, May 23, 2013 at 4:56 PM, Warren Weckesser < warren.weckesser@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... 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

Warren Weckesser <warren.weckesser <at> gmail.com> writes: [clip]
This is the variation of the decorator that I'll use: [clip]
Anything that does the job will probably do, but add it to scipy.lib and import it from there. We want something that can be recognized by a simple AST parser... -- Pauli Virtanen

On Thu, May 23, 2013 at 4:56 PM, Warren Weckesser <warren.weckesser@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__
Why do you attach to __func__? doesn't beta_gen.fit.__doc__ = rv_continuous.fit.__doc__ work. We are doing this in statsmodels, for example class BinaryModel(DiscreteModel) ... def fit_regularized(self, start_params=None, method='l1', ... fit_regularized.__doc__ = DiscreteModel.fit_regularized.__doc__ Is there a problem coming from the templated doc generation in stats.distributions? Josef
(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.
Warren
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-dev

On 5/25/13, josef.pktd@gmail.com <josef.pktd@gmail.com> wrote:
On Thu, May 23, 2013 at 4:56 PM, Warren Weckesser <warren.weckesser@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__
Why do you attach to __func__?
doesn't beta_gen.fit.__doc__ = rv_continuous.fit.__doc__ work.
We are doing this in statsmodels, for example
class BinaryModel(DiscreteModel) ... def fit_regularized(self, start_params=None, method='l1', ...
fit_regularized.__doc__ = DiscreteModel.fit_regularized.__doc__
Is there a problem coming from the templated doc generation in stats.distributions?
I was trying to set the __doc__ attribute after the definition of the class. But at that point, the __doc__ attribute is read-only. Your suggestion works--I just never tried that. Warren
Josef
(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.
Warren
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-dev
_______________________________________________ SciPy-Dev mailing list SciPy-Dev@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-dev
participants (3)
-
josef.pktd@gmail.com
-
Pauli Virtanen
-
Warren Weckesser