-1

I have a list of functions, say func_1, func_2, func_3, func_4, etc, now I would like to pass some variables x_1, x_2 as shared arguments to all of them. Question is, not all functions take exactly the same number of arguments. For example, func_1 and func_2 would take only x_1, while func_3 and func_4 take both x_1 and X_2. Is there a handy way to implement this on a scalable basis?

yuchenhu
  • 5
  • 1
  • I'm not sure exactly what you mean, but maybe making use of `*args` and `**kwargs` is what you're looking for? https://stackoverflow.com/questions/3394835/args-and-kwargs – Zevgon Nov 20 '17 at 07:42

1 Answers1

0

Since you have not posted any tried code so i have no idea what exactly you are looking.

But as you stated :

For example, func_1 and func_2 would take only x_1, while func_3 and func_4 take both x_1 and X_2

I am providing you Example solution, This is not exact solution You can take help, Hint from this solution:

def func1(*args):
    print("This is func1")
    return args

def func2(*args):
    print("This is func2")
    return args

def func3(*args):
    print("This is func3")
    return args

def func4(*args):
    print("This is func4")
    return args

func_list=[func1,func2,func3,func4]

argument_list=['x1','x2']

for func in func_list:
    if func==func1:
        print(func1(argument_list[0]))

    elif func==func2:
        print(func2(argument_list[0]))

    elif func==func3:
        print(func3(argument_list[0],argument_list[1]))
    else:
        print(func4(argument_list[0], argument_list[1]))

output:

This is func1
('x1',)
This is func2
('x1',)
This is func3
('x1', 'x2')
This is func4
('x1', 'x2')
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
  • Thanks! Had a hard time digesting *args and **kwargs. The codes helped a lot, I need only tailor the last part to serve my purpose. – yuchenhu Nov 20 '17 at 10:21