【问题标题】:Python: Pass function to function with named parametersPython:将函数传递给具有命名参数的函数
【发布时间】:2014-10-31 16:18:06
【问题描述】:

我希望将一个函数以及一个命名参数传递给另一个函数。这类似于the question asked here,只是它不处理命名参数。在 cmets 中提出了一个问题,但没有回复。

例子:

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'

【问题讨论】:

标签: python


【解决方案1】:

您只需要允许explore 也接收命名参数:

def printPath(path, displayNumber = False):
    pass

def explore(path, function, *args, **kwargs):
    contents = function(*args, **kwargs)

print explore(path, printPath, path, displayNumber = False)

【讨论】:

    猜你喜欢
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 2015-01-23
    • 1970-01-01
    • 2013-07-09
    相关资源
    最近更新 更多