我找到了这段代码并开始想,为什么不简单地将导入替换为自己的实现呢?使用 functools.partial 有什么(性能)好处吗?它是用纯 Python 还是本机代码实现的?
try:
from functools import partial
except ImportError:
# http://docs.python.org/library/functools.html#functools.partial
def partial(func, *args, **keywords):
def newfunc(*fargs, **fkeywords):
newkeywords = keywords.copy()
newkeywords.update(fkeywords)
return func(*(args + fargs), **newkeywords)
newfunc.func = func
newfunc.args = args
newfunc.keywords = keywords
return newfunc