I have some question:
How to call multiple functions (10,100, 1000 functions) with the same argument?
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? (Function_a, f2, add) So you cannot use:
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.
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
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With