Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Passing ints to a function
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
stayvoid  
View profile  
 More options Jun 8 2012, 7:41 pm
Newsgroups: comp.lang.python
From: stayvoid <stayv...@gmail.com>
Date: Fri, 8 Jun 2012 16:41:40 -0700 (PDT)
Local: Fri, Jun 8 2012 7:41 pm
Subject: Passing ints to a function
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Jun 8 2012, 7:56 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 08 Jun 2012 23:56:47 GMT
Local: Fri, Jun 8 2012 7:56 pm
Subject: Re: Passing ints to a function

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
stayvoid  
View profile  
 More options Jun 8 2012, 8:21 pm
Newsgroups: comp.lang.python
From: stayvoid <stayv...@gmail.com>
Date: Fri, 8 Jun 2012 17:21:38 -0700 (PDT)
Local: Fri, Jun 8 2012 8:21 pm
Subject: Re: Passing ints to a function

> You want to unpack the list:

> function(*a)  # like function(a[0], a[1], a[2], ...)

Awesome! I forgot about this.

Thanks.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jussi Piitulainen  
View profile  
 More options Jun 9 2012, 4:29 am
Newsgroups: comp.lang.python
From: Jussi Piitulainen <jpiit...@ling.helsinki.fi>
Date: 09 Jun 2012 11:29:13 +0300
Local: Sat, Jun 9 2012 4:29 am
Subject: Re: Passing ints to a function

stayvoid writes:
> > 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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rick Johnson  
View profile  
 More options Jun 10 2012, 5:41 pm
Newsgroups: comp.lang.python
From: Rick Johnson <rantingrickjohn...@gmail.com>
Date: Sun, 10 Jun 2012 14:41:58 -0700 (PDT)
Local: Sun, Jun 10 2012 5:41 pm
Subject: Re: Passing ints to a function
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 ;-)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »