将带有不同参数的函数传递给另一个函数时,如何避免过度使用 **kwargs?

发布于 2025-01-20 15:18:21 字数 1009 浏览 3 评论 0原文

我有一个像这样的函数:

def outer_function(input1, input2, func):
    a = ....[calculations that depend on input1 and input2]....
    b = ....[calculations that depend on input1 and input2]....
    c = ....[calculations that depend on input1 and input2]....
    d = func(a=a, b=b, c=c)

    return d

我可以将 func1func2 作为参数 func 传递到outer_function中。问题是 func1 实际上只依赖于 ab,因此可以写成这样

def func1(**kwargs):
    a = kwargs.get('a')
    b = kwargs.get('b')
    
    return something_with_a_and_b

同时,func2 取决于所有 3 个值,例如所以:

def func2(**kwargs):
    a = kwargs.get('a')
    b = kwargs.get('b')
    c = kwargs.get('c')
    return something_with_a_and_b_and_c

这可行,但我不喜欢它,因为它要求我将很多东西(在我的实际情况下)作为参数传递给 outer_function 中的 func实际例程并不总是使用它,所以它模糊了我的例程实际依赖的内容(相反,您必须查看我从 **kwargs 访问的值。是否有更好的方法来处理这种类型的情况?我有一个更清晰的类实现,但我需要在这种特定情况下的功能方法。

I have a function like so:

def outer_function(input1, input2, func):
    a = ....[calculations that depend on input1 and input2]....
    b = ....[calculations that depend on input1 and input2]....
    c = ....[calculations that depend on input1 and input2]....
    d = func(a=a, b=b, c=c)

    return d

where I can pass either func1 or func2 into the outer_function as the argument func. The problem is that func1 actually only depends on a and b, and so can be written like

def func1(**kwargs):
    a = kwargs.get('a')
    b = kwargs.get('b')
    
    return something_with_a_and_b

Meanwhile, func2 depends on all 3 values, like so:

def func2(**kwargs):
    a = kwargs.get('a')
    b = kwargs.get('b')
    c = kwargs.get('c')
    return something_with_a_and_b_and_c

This works, but I don't like it because it requires me to pass a lot of stuff (in my actual case) as arguments to func within outer_function that aren't always used by the actual routine, and so it obscures what my routines actually depend upon (and instead you have to look at what values I'm accessing from **kwargs. Is there a better way to handle this type of situation? I have a cleaner implementation as a class, but I need a functional approach in this specific situation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生未歇 2025-01-27 15:18:21

一种更清洁的方式,这是使装饰器处理自定义功能组成的方法。没有唯一的解决方案,因为它在组成顺序上逐渐消失。在这里示例

def a(i1, i2): return [i1**1 for i1 in range(i2)]
def b(i1, i2): return [i1**2 for i1 in range(i2)]
def c(i1, i2): return [i1**3 for i1 in range(i2)]

def power_of_2(data: list):
    return [i+2 for i in data]
    
def composer(input1, input2):
    def wrapper(*funcs):  # funcs are a, b, c, ...
        def wrapper2(func): # f is the final function
            return func(sum([f(input1, input2) for f in funcs], [])) # sum flat the list
        return wrapper2
    return wrapper

test1 = composer(3, 4)(a, b)(power_of_2)
print(test1)
test2 = composer(3, 4)(a, b, c)(power_of_2)
print(test2)

输出

[2, 3, 4, 5, 2, 3, 6, 11]
[2, 3, 4, 5, 2, 3, 6, 11, 2, 3, 10, 29]

A cleaner way to habdle this is make decorators to handle custom composition of functions. There is not a unique solution since it depdens on the order of composition. Here an example

def a(i1, i2): return [i1**1 for i1 in range(i2)]
def b(i1, i2): return [i1**2 for i1 in range(i2)]
def c(i1, i2): return [i1**3 for i1 in range(i2)]

def power_of_2(data: list):
    return [i+2 for i in data]
    
def composer(input1, input2):
    def wrapper(*funcs):  # funcs are a, b, c, ...
        def wrapper2(func): # f is the final function
            return func(sum([f(input1, input2) for f in funcs], [])) # sum flat the list
        return wrapper2
    return wrapper

test1 = composer(3, 4)(a, b)(power_of_2)
print(test1)
test2 = composer(3, 4)(a, b, c)(power_of_2)
print(test2)

Output

[2, 3, 4, 5, 2, 3, 6, 11]
[2, 3, 4, 5, 2, 3, 6, 11, 2, 3, 10, 29]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文