Python: Pass function to function with named parameters
我希望将一个函数传递给另一个函数,以及一个命名参数。这与这里提出的问题类似,只是它不涉及命名参数。在评论中有人问了一个问题,但没有答复。
例子:
1 2 3 4 5 6 7 | def printPath(path, displayNumber = False): pass def explore(path, function, *args): contents = function(*args) print explore(path, printPath, path, displayNumber = False) |
这就产生了错误:
1 | TypeError: explore() got an unexpected keyword argument 'displayNumber' |
您只需允许
1 2 3 4 5 6 7 | def printPath(path, displayNumber = False): pass def explore(path, function, *args, **kwargs): contents = function(*args, **kwargs) print explore(path, printPath, path, displayNumber = False) |