how to call a member function in python by its function instanceï¼
<p>Suppose we have the following code:</p>

<pre><code>class T(object):
  def m1(self, a):
    ...
f=T.m1
</code></pre>

<p>How to call f on an instance of T?</p>

<pre><code>x=T()
x.f(..)?
f(x, ..)?
</code></pre>

<p>A member function is just like any other function, except it takes <code>self</code> as the first argument and there is a mechanism which passes that argument automatically.</p>

<p>So, the short answer is, use it ths way:</p>

<pre><code>class T(object):
  def m1(self, a):
    pass

f=T.m1

x = T()

f(x, 1234)
</code></pre>

<h2>Unbound Method</h2>

<p>This is because you are using T.m1, which is an <em>"unbound method"</em>. Unbound here means that its <code>self</code> argument is not bound to an instance.</p>

<pre><code>&gt;&gt;&gt; T.m1
&lt;unbound method T.m1&gt;
</code></pre>

<h2>Bound Method</h2>

<p>Unlike <code>T.m1</code>, <code>x.m1</code> gives you a bound method:</p>

<pre><code>&gt;&gt;&gt; x.m1
&lt;bound method T.m1 of &lt;__main__.T object at 0x0000000002483080&gt;&gt;
</code></pre>

<p>You can reference a bound method and use it without passing <code>self</code> explicitly:</p>

<pre><code>f2 = x.m1
f2(1234)
</code></pre>

<h2>Bind using <code>partial</code></h2>

<p>You can also do the equivalent "binding" <code>self</code> yourself, with this code:</p>

<pre><code>import functools

unbound_f = T.m1
bound_f = functools.partial(unbound_f, x)

bound_f(1234)
</code></pre>

python