我正在寻找一种组合函数的方法,可以选择传递“额外”参数。
例子:
from toolz import compose_left
def g(a, b, c) -> int:
return a + b + c
def f(a, b = 100) -> int:
return a + b
当我这样做时:
compose_left(g, f)(1,2,3)
我明白了:
106
凉爽的。
但是有没有办法从compose中的函数g中定义参数b和c?
像这样:
compose_left(g(b=2,c=3), f)(1)
现在我明白了:
TypeError: g() missing 1 required positional argument: 'a'
我正在寻找可以执行此操作的函数或包。
您正在寻找functools.partial
.
compose_left(functools.partial(g, b=2, c=3), f)(1) # 106
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句