我想从初始值得到一系列计算结果.我实际上使用以下代码:
def function_composition(function_list, origin):
destination = origin
for func in function_list:
destination = func(destination)
return destination
Run Code Online (Sandbox Code Playgroud)
每个函数function_list都有一个参数.
我想知道python标准库中是否有类似的函数或更好的方法(例如:使用lambdas)来执行此操作.
Python中是否有一个函数可以执行此操作:
val = f3(f2(f1(arg)))
Run Code Online (Sandbox Code Playgroud)
通过键入此(例如):
val = chainCalling(arg,f3,f2,f1)
Run Code Online (Sandbox Code Playgroud)
我只是觉得,因为python(可以说)是一种函数式语言,我正在寻找的函数会使语法更明亮
我有几个字符串处理函数,例如:
def func1(s):
return re.sub(r'\s', "", s)
def func2(s):
return f"[{s}]"
...
Run Code Online (Sandbox Code Playgroud)
我想将它们组合成一个管道函数:my_pipeline(),以便我可以将其用作参数,例如:
class Record:
def __init__(self, s):
self.name = s
def apply_func(self, func):
return func(self.name)
rec = Record(" hell o")
output = rec.apply_func(my_pipeline)
# output = "[hello]"
Run Code Online (Sandbox Code Playgroud)
目标是用作my_pipeline参数,否则我需要一一调用这些函数。
谢谢。