0

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.

4
  • Do you want to pass x1 to a_func and x2 to b_func? Commented Nov 13, 2022 at 11:41
  • yes different values for different functions in a loop Commented Nov 13, 2022 at 11:44
  • Do these functions all take only one argument in your scenario? Commented Nov 13, 2022 at 11:45
  • No one of them has 7 variable and other just 2 Commented Nov 13, 2022 at 11:46

4 Answers 4

2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

1 Comment

as an example i wrote single variable but i have multivariable functions so passing both lists in order may not be the solution buti guess if i assign variables with 2d list it can work let me try
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.