Python 使用函数的输出作为其他函数的输入
我的软件是一组按顺序调用的函数:Python 使用函数的输出作为其他函数的输入,python,loops,functional-programming,Python,Loops,Functional Programming,我的软件是一组按顺序调用的函数: function1 -> function2 -> function3 .... 它们都收到相同的输入:一个名为options的字典 样本: def function1(options): # do something with options def function2(options): # do other thing with options def function3(options): # do other thi
function1 -> function2 -> function3 ....
它们都收到相同的输入:一个名为options
的字典
样本:
def function1(options):
# do something with options
def function2(options):
# do other thing with options
def function3(options):
# do other thing with options
有没有什么工具可以帮助我实现某种类型的功能
目前我正在使用循环:
flow = [function1,
function2,
function3]
options = {}
for phase in flow:
phase(options)
谢谢。这是你想要的吗
基于上面提到的信息,您当前的实现是完美的。为什么在为循环编写
时使用此选项更快、更高效、更清晰且可读?(哪个OP已经在使用)这对OP不起作用,因为所有这些函数都会原地改变arg,并且不返回任何值,这与Python中此类函数的常规情况相同。
def compose(*functions):
return functools.reduce(lambda f, g: lambda x: f(g(x)), functions, lambda x: x)