Passing a tuple to a function as multiple arguments

Discussion in 'Python' started by Ben Warren, Sep 7, 2007.

  1. Ben Warren

    Ben Warren Guest

    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
     
    Ben Warren, Sep 7, 2007
    #1
    1. Advertisements

  2. On Sep 6, 11:02 pm, Ben Warren <> wrote:
    > 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
     
    George Sakkis, Sep 7, 2007
    #2
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    999
    Larry Bates
    Apr 20, 2004
  2. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    1,164
    Antoon Pardon
    Mar 30, 2005
  3. Steve
    Replies:
    1
    Views:
    1,300
    Fredrik Lundh
    Dec 13, 2005
  4. Nathan Sokalski
    Replies:
    4
    Views:
    1,322
    Steve C. Orr [MVP, MCSD]
    Nov 20, 2006
  5. Replies:
    6
    Views:
    312
    Duncan Booth
    Feb 23, 2007
  6. jmborr
    Replies:
    1
    Views:
    635
    Stargaming
    Nov 3, 2007
  7. Davy
    Replies:
    3
    Views:
    2,053
    Wildemar Wildenburger
    Nov 7, 2007
  8. Nathan Sokalski

    Passing multiple arguments to the client-side JavaScript function in AJAX

    Nathan Sokalski, Nov 20, 2006, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    390
    Steve C. Orr [MVP, MCSD]
    Nov 20, 2006
Loading...