I want to pass several values to a function which is located on a
server (so I can't change its behavior).
That function only accepts five values which must be ints.
There are several lists:
a = [1, 2, 3, 4, 5]
b = [5, 4, 3, 2, 1]
c = [0, 0, 0, 0, 0]
I want to pass each value from these lists to that function.
What is the most pythonic way?
This wouldn't work because we're passing a list not ints:
function(a)
function(b)
function(c)
This wouldn't work too (the reason is the same):
def foo(x):
return x[0], x[1], x[2], x[3], x[4], x[5]
On Fri, 08 Jun 2012 16:41:40 -0700, stayvoid wrote:
> Hello,
> I want to pass several values to a function which is located on a server
> (so I can't change its behavior). That function only accepts five values
> which must be ints.
> There are several lists:
> a = [1, 2, 3, 4, 5]
> b = [5, 4, 3, 2, 1]
> c = [0, 0, 0, 0, 0]
> I want to pass each value from these lists to that function. What is the
> most pythonic way?
You want to unpack the list:
function(*a) # like function(a[0], a[1], a[2], ...)
> > function(*a) # like function(a[0], a[1], a[2], ...)
> Awesome! I forgot about this.
Here's something you could have thought of for yourself even when you
didn't remember that Python does have special built-in support for
applying a function to a list of arguments:
def five(func, args):
a, b, c, d, e = args
return func(a, b, c, d, e)
five(function, a)
five(function, b)
five(function, c)
for args in argses:
five(function, args)
The point is that the function itself can be passed as an argument to
the auxiliary function that extracts the individual arguments from the
list.
On Jun 9, 3:29 am, Jussi Piitulainen <jpiit...@ling.helsinki.fi>
wrote:
> Here's something you could have thought of for yourself even when you
> didn't remember that Python does have special built-in support for
> applying a function to a list of arguments:
> def five(func, args):
> a, b, c, d, e = args
> return func(a, b, c, d, e)
> The point is that the function itself can be passed as an argument to
> the auxiliary function that extracts the individual arguments from the
> list.
Good point. However the function "five" is much too narrowly defined
and the name is atrocious! I like concise, self-documenting
identifiers.