Take the 2-minute tour ×

I'm playing with Python callable. Basically you can define a python class and implement __call__ method to make the instance of this class callable. e.g.,

class AwesomeFunction(object):
    def __call__(self, a, b):
        return a+b

Module inspect has a function getargspec, which gives you the argument specification of a function. However, it seems I cannot use it on a callable object:

fn = AwesomeFunction()
import inspect

Unfortunately, I got a TypeError:

Traceback (most recent call last):
  File "/usr/lib/python2.6/inspect.py", line 803, in getargspec
    raise TypeError('arg is not a Python function')
TypeError: arg is not a Python function

I think it's quite unfortunate that you can't treat any callable object as function, unless I'm doing something wrong here?

share|improve this question
Thank you for reminding me of one of the lesser used and understood magic methods, __call__ –  Rafe Kettler Oct 22 '10 at 18:04
add comment

3 Answers

If you need this functionality, it is absolutely trivial to write a wrapper function that will check to see if fn has an attribute __call__ and if it does, pass its __call__ function to getargspec.

share|improve this answer
add comment

If you look at the definition of getargspec in the inspect module code on svn.python.org. You will see that it calls isfunction which itself calls:

isinstance(object, types.FunctionType)

Since, your AwesomeFunction clearly is not an instance of types.FunctionType it fails.

If you want it to work you should try the following:

share|improve this answer
I know it's expecting a function type. I'm just lamenting the fact that Python didn't design in a way that unifies functions and objects. –  EnToutCas Oct 22 '10 at 18:09
It's mainly because Python isn't an outrageously object-oriented language (Java is my best example). In Java, literally everything is an object, to the point where the smallest unit of runnable source is a class. In Python, since such a paradigm was eschewed, yes, a few features are missing, but it's generally clearer. –  Rafe Kettler Oct 22 '10 at 18:11
@EnToutCas: Write a proposal/patch. Might either A) be accepted, or B) shot down with a rationale that demonstrates why it's a bad idea. –  Nick T Oct 22 '10 at 18:15
@Nick, good idea. Instead of checking for function type, inspect.getargspec just have to check if the arg has the attribute __call__. Although I wouldn't imagine this being accepted as a patch... –  EnToutCas Oct 22 '10 at 18:26
add comment

__call__ defines something that can be called by a class instance. You're not giving getargspec a valid function because you're passing a class instance to it.

The difference between __init and __call__ is this:

fn = AwesomeFunction() # call to __init__
fn(1, 2) # call to __call__
share|improve this answer
add comment

Your Answer


