我希望将一个函数传递给另一个函数,以及一个命名参数。这与the question asked here类似,只是它没有解决命名参数。在评论中询问了一个问题,但没有回复。
示例:
def printPath(path, displayNumber = False):
pass
def explore(path, function, *args):
contents = function(*args)
print explore(path, printPath, path, displayNumber = False)
这给出了错误:
TypeError: explore() got an unexpected keyword argument 'displayNumber'
答案 0 :(得分:3)
您只需要允许explore
接收命名参数:
def printPath(path, displayNumber = False):
pass
def explore(path, function, *args, **kwargs):
contents = function(*args, **kwargs)
print explore(path, printPath, path, displayNumber = False)