calling functions from array but each function wants different value

Question:

similar topics have been opened, but I couldn’t find exactly what I wanted and couldn’t solve it myself.

What I want is to call my functions from an array in a loop, but the values ​​that each function needs are different. Sending all values ​​to all functions (they won’t use what they don’t need after all) or coding all 20 of my functions with if else is a solution but it seems like there must be a more efficient way.
Is there a solution? Thanks in advance for your help.

 def a_func(x1):
      return x1
 
 def b_func(y1):
      return y1

 mylist=['A','B']
 dictionary = {'A': a_func, 'B':b_func}

 def main(x1,y1):
      for each in mylist:
           value=dictionary[each](???) #x1 or x2 how can i send different variable according to each func

Actually, all the answers were indirect solutions, but ILS’s comment was working in different multi-functions, I needed to be more specific in the question, thank you very much, I understood the logic.

Asked By: alternative4

||

Answers:

this is the answer:

    def a_func(x1):
        return x1

    def b_func(y1):
        return y1

    funcs = [a_func, b_func]

    def main(inputs):
        x1 = 0
        y1 = 1
        inputs = [x1, y1] # suppose it is given from out of this function
        func_outputs = []
        for i in range(len(funcs)):
            func_output = funcs[i](inputs[i])
            func_outputs.append(func_output)
            print(func_output)

as an example:

    inputs = [0, 1]
    main(inputs)

output

    0
    1
Answered By: SadeghPouriyan

You can abuse **kwargs for this


def a_func(x1, **kwargs):
    return x1


def b_func(y1, **kwargs):
    return y1


mylist = ["A", "B"]
dictionary = {"A": a_func, "B": b_func}


def main(x1, y1):
    for each in mylist:
        value = dictionary[each](x1=x1, y1=y1)
        print(value)

main(1,2)

output

1
2
Answered By: Edo Akse

According to the comments, your actual situation is more complex.

Assuming you have a_func taking one argument, b_func taking two arguments, and you have three variables a, b, c to main. You want to pass a to a_func, and b, c to b_func. I think you need to group arguments by functions before passing them to main, i.e., calling main((a,), (b, c)). Then you can use zip to pass i-th argument group to i-th function.

def a_func(a0):
    return a0
def b_func(a0, a1):
    return (a0, a1)
func_list = [a_func, b_func]
def main(*argv):
    for func, arg in zip(func_list, argv):
        func(*arg)

main((a,), (b, c)) # In main, you call a_func(a) and b_func(b, c)
Answered By: ILS

A slight variation of @EdoAkse’s answer. If you cannot modify a_func/b_func, you can create a dictionary with lambdas:

def a_func(x1):
    return f"{x1=}"


def b_func(y1):
    return f"{y1=}"


mylist = ["A", "B"]
dictionary = {
    "A": lambda **kwargs: a_func(kwargs.get("x1")),
    "B": lambda **kwargs: b_func(kwargs.get("y1")),
}


def main(x1, y1):
    for each in mylist:
        value = dictionary[each](x1=x1, y1=y1)
        print(each, value)


main(10, 20)

Prints:

A x1=10
B y1=20
Answered By: Andrej Kesely
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.