title: Dynamically bind method to class instance in python
body: <p>Let's say that I have a class defined in <code>moduleA.py</code> which I want to add a method to, using some sort of loader method that takes a the name of a second module and the method defined there that should be bound</p>
  
  <pre><code>class ClassA(object):
      def __init__(self,config):
          super(ClassA, self).__init__()
  
          self.a = 1
          self.b = 2
          self.meth1 = self. bind_method(config)
  
      def bind_method(self,config):
          # load method
          &lt;return method defined in config as a str 'moduleB.meth2'&gt;
  
      def calling_method():
          return self.meth1() 
  </code></pre>
  
  <p>where the method defined in <code>moduleB.py</code> looks something like:</p>
  
  <pre><code>def meth2(self):
      return self.a + self.b
  </code></pre>
  
  <p>The point being that I want to be able to write <code>meth2</code> to be able to access class variables of <code>ClassA</code> once it is bound. This way, when you would have something like:</p>
  
  <pre><code>from moduleA import ClassA
  
  A = ClassA()
  aout = A.calling_method()
  </code></pre>
  
  <p>Calling <code>A.calling_method()</code> properly calls the method defined in <code>moduleB.py</code>.</p>
  
  <p>I've seen this sort of binding done in answers on SO after <code>ClassA</code> is instantiated using <code>types.MethodType</code>, but I haven't been able to dig up how to bind inside the class definition so that it is done internally when the class is instantiated. </p>
  
  <p>Any suggestions on what should go in the <code>bind_method</code> method would be much appreciated.</p>