Asher11 Asher11 - 5 months ago 7
Python Question

pass multiple parameters to function in multiple tuple sets - python

essentially I have a function which I need to pass to a bunch of values in two different moments (here skipped for practical reasons), hence, I thought need to split all the parameters in two tuple sets after which I'd pass the first set and then the second thinking - mistakenly - that python would allocate all the available n parameters of the function to the values passed with the tuple and then the rest with the second tuple. That is not the case:

def example_F_2(x, y, z, g):
return x * y + z * g

e = (1,2)
r = (3,4)
print(example_F_2(e, r))


in fact:

Traceback (most recent call last):
File "C:/Users/francesco/PycharmProjects/fund-analysis/test_sheet.py", line 7, in <module>
print(example_F_2(e, r))
TypeError: example_F_2() missing 2 required positional arguments: 'z' and 'g'


what can I do? I am guessing something from this page should work (https://docs.python.org/3.5/library/functools.html) but I have been unable to do it myself

Answer

A very simple way is to concatenate the tuples and then unpack them as you pass them to your function:

print(example_F_2(*(e+r)))