tomm
tomm

Reputation: 321

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?

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!

Upvotes: 3

Views: 2899

Answers (3)

John Coleman
John Coleman

Reputation: 52008

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:

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. Conceptually, this is what you are doing, though it is a matter of taste if you want to make it explicit in this way.

Upvotes: 1

superb rain
superb rain

Reputation: 5531

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

...

Upvotes: 9

Gábor Fekete
Gábor Fekete

Reputation: 1358

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. 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.

Upvotes: 1

Related Questions