我按照这个方法传递函数作为参数:
Passing functions with arguments to another function in Python?
然而,我无法理解如何将函数本身的参数作为names参数传递
def retry(func, *args):
func(*args)
def action(args):
do something
retry(action, arg1, namedArg=arg2)
这里我有个例外:
^{pr2}$通常,我可以:
action(arg1, namedArg=arg2)
请帮忙/
读这个,keyword arguments in python doc。在
因为错误清楚地指出
got an unexpected keyword argument 'namedArg'
。其中,as只提供*args中的参数。在你会发现很多例子来理解关键字参数。在
*args
及其同级的**kwargs
是通常用于额外参数和关键字参数的名称。传递namedArg=arg2
时传递的是一个kew-word参数。在所以,试试这个:
如果你用
^{pr2}$然后您将得到
args
作为参数列表,kwargs
作为关键字参数字典,因此在您的例子中你需要的是函数工具。在
http://docs.python.org/2/library/functools.html#functools.partial
相关问题 更多 >
编程相关推荐