Home > OS >  calling functions from array but each function wants different value
calling functions from array but each function wants different value

Time:11-13

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.

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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
  • Related