Dynamically add member function to an instance of a class in Python
<p>When I do the following:</p>

<pre><code>class C:
 pass

def f( self ):
 print self

a = C()

a.f = f

a.f()
</code></pre>

<p>I get the following error at the line a.f():
TypeError: f() takes exactly 1 argument (0 given)</p>

<p>The problem appears to be that when f is added to the instance, a, it is treated like a function that is stored inside of a, rather than an actual member function. If I change a.f() to a.f(a), then I get the intended effect, but is there a way to make python interpret the original example this way? In other words, is there a way to add a member function to an instance of a class at runtime?</p>

<p>Thanks</p>
