Can't get argspec for Python callables?

ぃ、小莉子 提交于 2019-12-05 01:22:13

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.

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:

inspect.getargspec(fn.__call__)

__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__
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!