Passing ints to a function

S

stayvoid

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
 
S

Steven D'Aprano

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)
 
J

Jussi Piitulainen

stayvoid said:
You want to unpack the list:

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.
 
R

Rick Johnson

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 ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top