简体   繁体   English

如何传递“额外”参数来编写 function

[英]How to pass “extra” parameter to compose function

I' am searching for a way to compose function with an option to pass 'extra' parameter.我正在寻找一种方法来组合 function 并带有传递“额外”参数的选项。

Example:例子:

from toolz import compose_left

def g(a, b, c) -> int:
    return a + b + c

def f(a, b = 100) -> int:
    return a + b

when i do this:当我这样做时:

compose_left(g, f)(1,2,3)

i get this:我明白了:

106

cool.凉爽的。

but is there a way to define the parameter b and c from the function g in compose?但是有没有办法在compose中从function g中定义参数b和c?

like this:像这样:

compose_left(g(b=2,c=3), f)(1)

now i get this:现在我明白了:

TypeError: g() missing 1 required positional argument: 'a'

I am looking for a function or a package that can do this.我正在寻找可以做到这一点的 function 或 package。

You're looking for functools.partial .您正在寻找functools.partial

compose_left(functools.partial(g, b=2, c=3), f)(1)  # 106

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM