python - Python 复杂参数传递需求的解决方案是什么?

标签 python parameter-passing

我正在寻找以下复杂参数传递问题的解决方案:

我想使用 Python 将函数的列表及其参数作为另一个函数的参数传递。我知道可以将函数作为参数传递, 但是是否可以在 python 中传递函数及其参数的列表?

我的示例代码:

self.myObject = Column(self.orderedColumnDictionary, \ 
   fillingOutMethods = [[firstFillingOutMethod, parameter1], \
   [anotherFillOutMethod, parameter2, parameter3]])

在这段代码中,我正在初始化 Column 类的一个对象。因此,在创建对象时,我想传递各种函数作为参数。我正在考虑将该对象所需的所有函数作为 lits 传递。例如,在此示例代码中,我的函数是firstFillingOutMethod,其中我将parameter1作为参数传递,另一个函数是 anotherFillOutMethod,我想将参数 2 和参数 3 作为参数传递。

因此,我期待任何执行此类任务的建议。

谢谢

最佳答案

这是一个例子:

def f1(a):
    return a*a

def f2(a,b):
    return a*b

flist = [[f1, 2], [f2, 3, 4]]

print [item[0](*item[1:]) for item in flist]

输出是:

[4, 12]

关于python - Python 复杂参数传递需求的解决方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6895433/

相关文章:

python - 解析文件以收集节标题和后续内容

python pandas删除重复的列

python - 错误:how to fix the swift error type to my server

c# - 如何使用 where @para1 = @para2 等参数执行 Sql server 过程

c - 使用 `option long_options[]` 时了解 `getopt_long`

R 检查参数是否定义

powershell - "Cannot bind parameter because Param2 is specified more than once"

javascript - 使用 angular8 在循环中传递参数 onclick

python - 强制从 Python 标准库导入模块而不是默认的 PYTHONPATH

python - TensorFlow:卡住图形后准确性急剧下降?