Passing a tuple to a function as multiple arguments

B

Ben Warren

Hello,

Let's say I have a function with a variable number of arguments (please ignore syntax errors):

def myfunc(a,b,c,d,...):

and I have a tuple whose contents I want to pass to the function. The number of elements in the tuple will not always be the same.

T = A,B,C,D,...

Is there a way that I can pass the contents of the tuple to the function without explicitly indexing the elements? Something like:

myfunc(magic(T))

where magic() expands the tuple to its multiple elements. I've tried various for loop constructs, but everything I can think of returns a list, which doesn't work because it's viewed as one parameter.


thanks,
Ben
 
G

George Sakkis

Hello,

Let's say I have a function with a variable number of arguments (please ignore syntax errors):

def myfunc(a,b,c,d,...):

and I have a tuple whose contents I want to pass to the function. The number of elements in the tuple will not always be the same.

T = A,B,C,D,...

Is there a way that I can pass the contents of the tuple to the function without explicitly indexing the elements?

Yes:

myfunc(*T)

More details at http://docs.python.org/tut/node6.html#SECTION006730000000000000000


HTH,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top