1

I am trying to add a method to an existing Python scipy.stats class but it is generating a _construct_docstrings error.

import scipy.stats as stats

class myPoisson(stats.poisson) :
  def myMethod(var, **kwargs) :
    return var

I have tried adding an __init__ method with a call to super().__init__(self) but this has not changed the error.

What am I missing for extending existing Python classes?

Edmund
  • 488
  • 4
  • 21

2 Answers2

1

hopefully the following example helps you out.

def myMethod(var, **kwargs):
    return var

stats.poisson.myMethod = myMethod

stats.poisson.myMethod(2)

Refer to Adding a Method to an Existing Object Instance for further details on the topic.

dusio
  • 480
  • 5
  • 18
0

Scipy.stats distributions are instances, not classes (for historic reasons). Thus, you need to inherit from e.g. poisson_gen, not poisson. Better still, inherit from rv_continuous or rv_discrete directly. See the docstring of rv_continuous for some info on subclassing distributions

ev-br
  • 24,968
  • 9
  • 65
  • 78