【发布时间】:2010-10-22 17:39:52
【问题描述】:
我正在使用 Python 可调用对象。基本上你可以定义一个 python 类并实现__call__ 方法来使这个类的实例可调用。例如,
class AwesomeFunction(object):
def __call__(self, a, b):
return a+b
Module inspect 有一个函数 getargspec,它给你一个函数的参数说明。但是,似乎我不能在可调用对象上使用它:
fn = AwesomeFunction()
import inspect
inspect.getargspec(fn)
不幸的是,我收到了一个 TypeError:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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
我认为很遗憾你不能将任何可调用对象视为函数,除非我在这里做错了什么?
【问题讨论】:
-
感谢您提醒我一种较少使用和理解的魔术方法,
__call__
标签: python reflection inspection