title: Python inherit method from arbitrary class
body: <p>I'm sure this will be a duplicate question, but I can't seem to find the words to locate one.</p>
  
  <p>I have a set of very similar models I'd like to code up. The models are all the same, apart from a single function / line of code. I'd like to avoid any code repetition. Let' see an MWE:</p>
  
  <pre><code>import numpy as np
  
  class SinModel:   
      def __init__(self):
          self.x = np.linspace(-np.pi, np.pi)
      def run(self):
          # Computations which are invariant of the function we use later
          self.y = np.sin(self.x)
          # More computations which are invariant of which funcion was used
  </code></pre>
  
  <p>Our second model will involve the same series of computations, but will use a different function mid way though (here, cosine instead of sine):</p>
  
  <pre><code>class CosModel:
      def __init__(self):
          self.x = np.linspace(-np.pi, np.pi)
      def run(self):
          # Computations which are the same as in SinModel
          self.y = np.cos(self.x)
          # More computations which are the same as in SinModel
  </code></pre>
  
  <p>Here I have lots of code repetition. Is there a better way to implement these models? I was hoping it would be possible to create a class <code>Model</code> which could inherit the differing function from an arbitrary class.</p>
  
  <p>An important note is that the function which changes between models may take different arguments from <code>self</code> depending on the model.</p>