你问它是否真的是一个函数,它告诉你不是一个函数。它是一个方法包装器。在>>> def printargs(*args):
... print args
>>> import inspect
>>> from functools import partial
>>> inspect.isfunction(printargs)
True
>>> f = partial(printargs, 1)
>>> inspect.isfunction(f)
False
# try duck-typing, see if the variable is callable
# check does it work for a method-wrapper?
>>> callable(f)
True
# check an integer, which should be false
>>> callable(1)
False
# ensure it works on an actual function
>>> callable(printargs)
True
这就是为什么你喜欢鸭子。你不在乎它是不是函数。你在乎它是否像一个函数。在
编辑:
如果绝望的话,你可以编写一个类并传递对类中函数的引用。在
^{pr2}$
那就用(f)调用作为你的包装。在>>> f_ = A(f)
>>> inspect.ismethod(f_.call)
True
>>> f_.call()
(1,)
只要ismethod有效,这个方法就有效。在
如果没有,你真的需要一个装修工。在
最终编辑:
如果您真的非常绝望,并且不想编写定制的decorator,那么可以使用一个带有元组的lambda函数来生成一个部分类似的函数。在
例如:>>> import inspect
>>> def printargs(*args):
... print args
>>> a = (1,2,3)
>>> f = lambda x: printargs(*x)
>>> f(a)
(1, 2, 3)
>>> inspect.isfunction(f)
True