title: How to find out the arity of a method in Python
body: <p>I'd like to find out the arity of a method in Python (the number of parameters that it receives).
  Right now I'm doing this:</p>
  
  <pre><code>def arity(obj, method):
    return getattr(obj.__class__, method).func_code.co_argcount - 1 # remove self
  
  class Foo:
    def bar(self, bla):
      pass
  
  arity(Foo(), "bar")   # =&gt; 1
  </code></pre>
  
  <p>I'd like to be able to achieve this:</p>
  
  <pre><code>Foo().bar.arity()   # =&gt; 1
  </code></pre>
  
  <p><strong>Update</strong>: Right now the above function fails with built-in types, any help on this would also be appreciated:</p>
  
  <pre><code> # Traceback (most recent call last):
   #   File "bla.py", line 10, in &lt;module&gt;
   #     print arity('foo', 'split')  # =&gt;
   #   File "bla.py", line 3, in arity
   #     return getattr(obj.__class__, method).func_code.co_argcount - 1 # remove self
   # AttributeError: 'method_descriptor' object has no attribute 'func_co
  </code></pre>