Python: calling a function as a method of a class
<p>Let's start with some code:</p>

<pre><code>def func(*x):
    print('func:', x)


class ABC:
    def __init__(self, f):
        self.f1 = f

    def f2(*x):
        print('f2:', x)
</code></pre>

<p>Now we do some tests:</p>

<pre><code>&gt;&gt;&gt; a = ABC(func)
&gt;&gt;&gt; a.f1(10)
func: (10,)
&gt;&gt;&gt; a.f2(10)
f2: (&lt;__main__.ABC object at 0xb75381cc&gt;, 10)
&gt;&gt;&gt; a.f3 = func
&gt;&gt;&gt; a.f3(10)
func: (10,)
&gt;&gt;&gt; a.f1
&lt;function func at 0xb74911ec&gt;
&gt;&gt;&gt; a.f2
&lt;bound method ABC.f2 of &lt;__main__.ABC object at 0xb75381cc&gt;&gt;
&gt;&gt;&gt; a.f3
&lt;function func at 0xb74911ec&gt;
</code></pre>

<p>Note that <code>func</code> is a normal function and we are making it a method <code>f1</code> of the class.</p>

<p>We can see that <code>f2</code> is getting the class instance as the first argument, but <code>f1</code> and <code>f3</code> are not, even though all functions are called as class methods. We can also see that if we call a normal function as a method of a class, Python does not make a bound method from it.</p>

<p>So why is <code>f1</code> or <code>f3</code> NOT getting a class instance passed to it even when we are calling it as a method of a class? And also, how does Python know that we are calling an outer function as a method so that it should not pass an instance to it.</p>

<p><strong>-- EDIT --</strong></p>

<p>OK, so basically what I am doing wrong is that I am attaching the functions on the instance and NOT on the class object itself. These functions therefore simply become instance attributes. We can check this with:</p>

<pre><code>&gt;&gt;&gt; ABC.__dict__
... contents...
&gt;&gt;&gt; a.__dict__
{'f1': &lt;function func at 0xb74911ec&gt;, 'f3': &lt;function func at 0xb74911ec&gt;}
</code></pre>

<p>Also note that this dict can not be assigned to:</p>

<pre><code>&gt;&gt;&gt; ABC.__dict__['f4'] = func
TypeError: 'dict_proxy' object does not support item assignment
</code></pre>
