title: Slighly alter method by passing function to class constructor in Python
body: <p>I'm writing a class which needs to use slightly different code depending on the instance.  </p>
  
  <p>I could achieve this through inheritance, but I wondered whether it would would be simpler to do this by passing a function to constructor when an instance is instantiated.  </p>
  
  <p>I'm thinking something like this:</p>
  
  <pre><code>class myClass():
  
      def __init__(self, fn):
          self.myProperty = 1
          self.instanceSpecificFunction = fn
  
      def myMethod(self):
  
          #Do things here that are common to all instances of class
  
          #[common code would be here]
  
          #But then run some code that is specific to this instance of the class
          self.instanceSpecificFunction(self)
  
  def functionThatsSpecificToThisInstance(self):
  
      #In general, this code would want 
      #to be different if we instantiated a different instance
  
      self.myProperty = 3
  
  myInstance = myClass(functionThatsSpecificToThisInstance)  
  
  myInstance.myMethod()
  </code></pre>
  
  <p>Is this a terrible idea?  Does this kind of design pattern have a name?  If so is there a 'best' way to this?</p>
  
  <p>Thanks very much,</p>
  
  <p>Robin</p>