title: Class variable reference to function changes to instancemethod
body: <p>I'm trying to call an external function via a class variable. The following is a simplification of my real code:</p>
  <pre class="lang-py prettyprint-override"><code>def func(arg):
      print(arg)
  
  class MyClass(object):
      func_ref = None
  
      @classmethod
      def setUpClass(cls):
          #MyClass.func_ref = func
          cls.func_ref = func
  
      @staticmethod
      def func_override(arg):
          print(&quot;override printing arg...&quot;)
          MyClass.func_ref(arg)
  
  if __name__ == &quot;__main__&quot;:
      print(type(func))
      print(type(MyClass.func_ref))
      MyClass.setUpClass()
      print(type(MyClass.func_ref))
      MyClass.func_override(&quot;hello!&quot;)
  </code></pre>
  <p>The above code produces the following output:</p>
  <pre><code>[~]$ python tmp.py
  &lt;type 'function'&gt;
  &lt;type 'NoneType'&gt;
  &lt;type 'instancemethod'&gt;
  override printing arg...
  Traceback (most recent call last):
    File &quot;tmp.py&quot;, line 20, in &lt;module&gt;
      MyClass.func_override(&quot;hello!&quot;)
  TypeError: func_override() takes exactly 2 arguments (1 given)
  </code></pre>
  <p>The situation seems to be unchanged if I use <code>MyClass</code> in place of <code>cls</code> within the classmethod <code>setUpClass()</code>.</p>
  <p>I would expect the type of <code>MyClass.func_ref</code> to be <code>function</code> after the assignment in <code>setUpClass()</code> which explains the <code>TypeError</code> I get when I try to call it. Why is the type of <code>func_ref</code> being changed to <code>instancemethod</code> when the value I assigned to it is of type <code>function</code>?</p>
  <p>This only seems to be an issue in Python 2. Python 3 behaves as I would expect.</p>
  <p>How do I get calls to the static method <code>MyClass.func_override()</code> to call <code>func()</code>?</p>
  <p>UPDATE</p>
  <p>I was able to get the above to work by applying the following patch:</p>
  <pre><code>@@ -14,7 +14,7 @@ class MyClass(object):
       def func_override(arg):
           print(&quot;override printing arg...&quot;)
           func(arg)
  -        MyClass.func_ref.__func__(arg)
  +        MyClass.func_ref(arg)
  
   if __name__ == &quot;__main__&quot;:
       print(type(func))
  </code></pre>
  <p>While the above works, its not at all clear to me why I needed to do this. I still don't understand why the type of <code>func_ref</code> ends up an <code>instancemethod</code> when I assigned to it a value of type <code>function</code>.</p>