passing variable arguments to a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan Wilcox

    #1

    passing variable arguments to a function

    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(*arg s):
    print args

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

    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

  • Christophe

    #2
    Re: passing variable arguments to a function

    Ryan Wilcox a écrit :[color=blue]
    > 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(*arg s):
    > print args
    >
    > myList = [1, 2, 3, 4]
    > myFunction(myLi st)
    >
    > 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
    >[/color]

    myFunction(*myL ist)

    Comment

    • Fredrik Lundh

      #3
      Re: passing variable arguments to a function

      Ryan Wilcox wrote:
      [color=blue]
      > 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(*arg s):
      > print args
      >
      > myList = [1, 2, 3, 4]
      > myFunction(myLi st)
      >
      > 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?[/color]

      same syntax:

      myFunction(*myL ist)

      </F>



      Comment

      Working...