简体   繁体   English

如何正确调用具有相同参数的多个函数?

[英]How to properly call many functions with the same args?

I have some question:我有一个问题:

How to call multiple functions (10,100, 1000 functions) with the same argument?如何使用相同的参数调用多个函数(10,100、1000 个函数)?

Just an example:只是一个例子:

def function1(arg):
return arg

def function2(arg):
    return arg*2

def function_add_arg(arg):
    return np.sum(arg)


def call_functions(values):

    result = (function1(values), function2(values), function_add_arg(values))
    return result

values = [1, 2, 3, 4]
result_tuple = call_functions(values)

What if I have 1000 functions with different names?如果我有 1000 个不同名称的函数怎么办? (Function_a, f2, add) So you cannot use: (Function_a, f2, add) 所以你不能使用:

result = (eval(f'function_{i}({values})') for i in range(100))

My solution for this example (it's not the best one, it's just for showing my idea):我对这个例子的解决方案(它不是最好的,它只是为了展示我的想法):

def call_functions(function_list, values):
    result = tuple((function(values) for function in function_list))
    return result

function_list = [function1, function2, function_add_arg]
values = [1, 2, 3, 4]

result_tuple = call_functions(function_list, values)

But how do it correctly?但是如何正确地做到这一点? (especially if I will call more functions) (特别是如果我会调用更多函数)

Different solution is to use **kwargs except function list.不同的解决方案是使用 **kwargs 除了 function 列表。

Some different, better solutions?一些不同的,更好的解决方案? Decorators?装饰师?

Regards!问候!

You could build that list of functions with a decorator:您可以使用装饰器构建该函数列表:

function_list = []
def register(function):
    function_list.append(function)
    return function

@register
def function1(arg):
    return arg

@register
def function2(arg):
    return arg*2

...

One way to do it is to put all your functions into a separate file with an additional line at the end:一种方法是将所有函数放入一个单独的文件中,并在末尾添加一行:

def f1(arg):
    ...
def f2(arg):
    ...
.
.
.
def fN(arg):
    ...

functions = {k:v for k,v in vars().items() if '__' not in k}

The functions variable will contain the name of all your functions that were declared before. functions变量将包含之前声明的所有函数的名称。 I removed the built-in variables that start and end with the double underscores but you can also filter it for a prefix that all your functions are using.我删除了以双下划线开头和结尾的内置变量,但您也可以过滤它以获得所有函数都使用的前缀。 Importing this file will also import the variable that you can use to iterate through and call them with your arguments.导入此文件还将导入可用于迭代并使用 arguments 调用它们的变量。

You could write a higher-order function which fixes an input and makes the return value a function of the function that you apply to that input:您可以编写一个高阶 function 来修复输入并使返回值成为您应用于该输入的 function 的 function:

def fix_input(value):
    def apply(f):
        return f(value)
    return apply

Used like:像这样使用:

def function1(arg):
    return arg

def function2(arg):
    return arg*2

def function_add_arg(arg):
    return sum(arg)

function_list = [function1, function2, function_add_arg]
values = [1, 2, 3, 4]

apply = fix_input(values)
print([apply(f) for f in function_list])
#[[1, 2, 3, 4], [1, 2, 3, 4, 1, 2, 3, 4], 10]

You could also use apply with map , allowing you to map an input over a list of functions.您还可以applymap一起使用,允许您通过函数列表对map进行输入。 Conceptually, this is what you are doing, though it is a matter of taste if you want to make it explicit in this way.从概念上讲,这就是您正在做的事情,尽管如果您想以这种方式使其明确,这是一个品味问题。

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

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