Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing ints to a function

62 views
Skip to first unread message

stayvoid

unread,
Jun 8, 2012, 7:41:40 PM6/8/12
to
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?

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]

function(foo(a))
function(foo(b))
function(foo(c))

This would work, but it's not pythonic at all (imagine that you want
to call it 10 times or even more).

function(a[0], a[1], a[2], a[3], a[4], a[5])
function(b[0], b[1], b[2], b[3], b[4], b[5])
function(c[0], c[1], c[2], c[3], c[4], c[5])

Thanks







Steven D'Aprano

unread,
Jun 8, 2012, 7:56:47 PM6/8/12
to
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], ...)

If you have many lists, use a for-loop:

for L in (a, b, c):
function(*L)




--
Steven

stayvoid

unread,
Jun 8, 2012, 8:21:38 PM6/8/12
to
> You want to unpack the list:
>
> function(*a)  # like function(a[0], a[1], a[2], ...)

Awesome! I forgot about this.

Thanks.

Jussi Piitulainen

unread,
Jun 9, 2012, 4:29:13 AM6/9/12
to
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.

Rick Johnson

unread,
Jun 10, 2012, 5:41:58 PM6/10/12
to
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.

py> L5 = [1, 2, 3, 4, 5]
py> L4 = [1, 2, 3, 4]
py> def f4(a,b,c,d):
print a,b,c,d
py> def f5(a,b,c,d,e):
print a,b,c,d,e
py> def apply_five(func, args):
a, b, c, d, e = args
return func(a, b, c, d, e)
py> apply_five(f5, L5)
1 2 3 4 5
py> apply_five(f5, L4)
ValueError: need more than 4 values to unpack
#
# Try this instead:
#
py> def apply_arglst(func, arglst):
return func(*arglst)
py> apply_arglst(f4,L4)
1 2 3 4
py> apply_arglst(f5,L5)
1 2 3 4 5

...of course you could create a general purpose apply function; like
the one Python does not possess any longer ;-)
0 new messages