passing variable arguments to a function

Discussion in 'Python' started by Ryan Wilcox, Oct 13, 2005.

  1. Ryan Wilcox

    Ryan Wilcox Guest

    Hello all,

    I want to be able to pass a variable number of parameters into a Python
    function. Now, I know how to _receive_ variable arguments, but I don't
    know how to _send_ them.

    def myFunction(*args):
    print args

    myList = [1, 2, 3, 4]
    myFunction(myList)

    this function will print out ([1, 2, 3, 4]).

    Except that's not what I want. I want the equivalent to:

    myFunction(1, 2, 3, 4)

    So, given an array, how can I unpack the array and pass all of the
    elements into a Python function as parameters?

    Thanks in advance!,
    _Ryan Wilcox
    Ryan Wilcox, Oct 13, 2005
    #1
    1. Advertising

  2. Ryan Wilcox

    Christophe Guest

    Ryan Wilcox a écrit :
    > Hello all,
    >
    > I want to be able to pass a variable number of parameters into a Python
    > function. Now, I know how to _receive_ variable arguments, but I don't
    > know how to _send_ them.
    >
    > def myFunction(*args):
    > print args
    >
    > myList = [1, 2, 3, 4]
    > myFunction(myList)
    >
    > this function will print out ([1, 2, 3, 4]).
    >
    > Except that's not what I want. I want the equivalent to:
    >
    > myFunction(1, 2, 3, 4)
    >
    > So, given an array, how can I unpack the array and pass all of the
    > elements into a Python function as parameters?
    >
    > Thanks in advance!,
    > _Ryan Wilcox
    >


    myFunction(*myList)
    Christophe, Oct 13, 2005
    #2
    1. Advertising

  3. Ryan Wilcox wrote:

    > I want to be able to pass a variable number of parameters into a Python
    > function. Now, I know how to _receive_ variable arguments, but I don't
    > know how to _send_ them.
    >
    > def myFunction(*args):
    > print args
    >
    > myList = [1, 2, 3, 4]
    > myFunction(myList)
    >
    > this function will print out ([1, 2, 3, 4]).
    >
    > Except that's not what I want. I want the equivalent to:
    >
    > myFunction(1, 2, 3, 4)
    >
    > So, given an array, how can I unpack the array and pass all of the
    > elements into a Python function as parameters?


    same syntax:

    myFunction(*myList)

    </F>
    Fredrik Lundh, Oct 13, 2005
    #3
    1. Advertising

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. Replies:
    5
    Views:
    515
  2. jmborr
    Replies:
    1
    Views:
    413
    Stargaming
    Nov 3, 2007
  3. Replies:
    3
    Views:
    821
  4. arnuld
    Replies:
    21
    Views:
    852
    Richard Bos
    Aug 4, 2009
  5. Navaneeth
    Replies:
    4
    Views:
    550
    Kenny McCormack
    Nov 20, 2010
Loading...

Share This Page