0 votes
1 view
in Python by (12.5k points)

Is there a way to expand a Python tuple into a function - as actual parameters?

For example, here expand() does the magic:

tuple = (1, "foo", "bar")

def myfun(number, str1, str2):

       return (number * 2, str1 + str2, str2 + str1) myfun(expand(tuple)) # (2, "foobar", "barfoo")

I know one could define myfun as myfun((a, b, c)), but of course there may be legacy code. Thanks

1 Answer

0 votes
by (47.3k points)

To expand a Python tuple into a function - as actual parameters, you can use the following method:-

myfun(*tuple) 

Example:

tuple = (1, "foo", "bar")

def myfun(number, str1, str2):

  return (number * 2, str1 + str2, str2 + str1)

myfun(*tuple)

This gives the same output that you want.

...