Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > passing variable arguments to a function

Reply
Thread Tools

passing variable arguments to a function

 
 
Ryan Wilcox
Guest
Posts: n/a
 
      10-13-2005
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

 
Reply With Quote
 
 
 
 
Christophe
Guest
Posts: n/a
 
      10-13-2005
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)
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-13-2005
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>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a function that expects variable arguments from a functionwith variable arguments Navaneeth C Programming 4 11-20-2010 05:35 AM
passing variable number of arguments to a function arnuld C Programming 21 08-04-2009 09:33 PM
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
Passing a variable number of arguments to a wrapped function. stephen@theboulets.net Python 5 08-05-2005 07:31 PM



Advertisments