[SciPy-Dev] Reusing docstrings of the parent class in overridden methods of a subclass

Warren Weckesser warren.weckesser at gmail.com
Sat May 25 09:42:08 EDT 2013


On 5/25/13, josef.pktd at gmail.com <josef.pktd 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__
>
> 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 at scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-dev
>>
> _______________________________________________
> SciPy-Dev mailing list
> SciPy-Dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-dev
>



More information about the SciPy-Dev mailing list