Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.python

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 08-09-2012, 12:41 AM
bruceg113355@gmail.com
Guest
 
Posts: n/a
Default Is there a clever way to pass arguments

Is there a way in Python to pass arguments without listing each argument?
For example, my program does the following:

testData (z[0], z[1], z[2], z[3], z[4], z[5], z[6], z[7])

Is there a clever way to pass arguments in a single statement knowing that each argument is a sequential index from a list?
I cannot change the function definition.

Thanks,
Bruce
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 08-09-2012, 12:47 AM
Andrew Cooper
Guest
 
Posts: n/a
Default Re: Is there a clever way to pass arguments

On 09/08/2012 01:41, bruceg113355@gmail.com wrote:
> Is there a way in Python to pass arguments without listing each argument?
> For example, my program does the following:
>
> testData (z[0], z[1], z[2], z[3], z[4], z[5], z[6], z[7])
>
> Is there a clever way to pass arguments in a single statement knowing that each argument is a sequential index from a list?
> I cannot change the function definition.
>
> Thanks,
> Bruce
>


testData(*z)

assuming that there are exactly 8 entries in z

see
http://docs.python.org/dev/tutorial/...ning-functions

~Andrew
Reply With Quote
  #3 (permalink)  
Old 08-09-2012, 01:07 AM
Dave Angel
Guest
 
Posts: n/a
Default Re: Is there a clever way to pass arguments

On 08/08/2012 08:41 PM, bruceg113355@gmail.com wrote:
> Is there a way in Python to pass arguments without listing each argument?
> For example, my program does the following:
>
> testData (z[0], z[1], z[2], z[3], z[4], z[5], z[6], z[7])
>
> Is there a clever way to pass arguments in a single statement knowing that each argument is a sequential index from a list?
> I cannot change the function definition.
>
> Thanks,
> Bruce

If a function is expecting exactly 8 arguments, and z is a list of
length 8, you can call the function like:

testData(*z)

if z is longer, then you'd need something like (untested)
testData(*z[:8])

The * basically turns a list into separate arguments, and these are then
applied to the formal parameters.

--

DaveA

Reply With Quote
  #4 (permalink)  
Old 08-09-2012, 01:20 AM
bruceg113355@gmail.com
Guest
 
Posts: n/a
Default Re: Is there a clever way to pass arguments

On Wednesday, August 8, 2012 9:07:04 PM UTC-4, Dave Angel wrote:
> On 08/08/2012 08:41 PM, bruceg113355@gmail.com wrote:
>
> > Is there a way in Python to pass arguments without listing each argument?

>
> > For example, my program does the following:

>
> >

>
> > testData (z[0], z[1], z[2], z[3], z[4], z[5], z[6], z[7])

>
> >

>
> > Is there a clever way to pass arguments in a single statement knowing that each argument is a sequential index from a list?

>
> > I cannot change the function definition.

>
> >

>
> > Thanks,

>
> > Bruce

>
> If a function is expecting exactly 8 arguments, and z is a list of
>
> length 8, you can call the function like:
>
>
>
> testData(*z)
>
>
>
> if z is longer, then you'd need something like (untested)
>
> testData(*z[:8])
>
>
>
> The * basically turns a list into separate arguments, and these are then
>
> applied to the formal parameters.
>
>
>
> --
>
>
>
> DaveA









Dave, your solution works!

def testData (z0, z1, z2, z3, z4, z5, z6, z7):
print (z0, z1, z2, z3, z4, z5, z6, z7)

z = []
z.append(0)
z.append(1)
z.append(2)
z.append(3)
z.append(4)
z.append(5)
z.append(6)
z.append(7)

testData(*z[:8])

Thank you,
Bruce
Reply With Quote
  #5 (permalink)  
Old 08-09-2012, 01:20 AM
bruceg113355@gmail.com
Guest
 
Posts: n/a
Default Re: Is there a clever way to pass arguments

On Wednesday, August 8, 2012 9:07:04 PM UTC-4, Dave Angel wrote:
> On 08/08/2012 08:41 PM, bruceg113355@gmail.com wrote:
>
> > Is there a way in Python to pass arguments without listing each argument?

>
> > For example, my program does the following:

>
> >

>
> > testData (z[0], z[1], z[2], z[3], z[4], z[5], z[6], z[7])

>
> >

>
> > Is there a clever way to pass arguments in a single statement knowing that each argument is a sequential index from a list?

>
> > I cannot change the function definition.

>
> >

>
> > Thanks,

>
> > Bruce

>
> If a function is expecting exactly 8 arguments, and z is a list of
>
> length 8, you can call the function like:
>
>
>
> testData(*z)
>
>
>
> if z is longer, then you'd need something like (untested)
>
> testData(*z[:8])
>
>
>
> The * basically turns a list into separate arguments, and these are then
>
> applied to the formal parameters.
>
>
>
> --
>
>
>
> DaveA









Dave, your solution works!

def testData (z0, z1, z2, z3, z4, z5, z6, z7):
print (z0, z1, z2, z3, z4, z5, z6, z7)

z = []
z.append(0)
z.append(1)
z.append(2)
z.append(3)
z.append(4)
z.append(5)
z.append(6)
z.append(7)

testData(*z[:8])

Thank you,
Bruce
Reply With Quote
  #6 (permalink)  
Old 08-09-2012, 01:34 AM
Steven D'Aprano
Guest
 
Posts: n/a
Default Re: Is there a clever way to pass arguments

On Wed, 08 Aug 2012 18:20:40 -0700, bruceg113355 wrote:

> z = []
> z.append(0)
> z.append(1)
> z.append(2)
> z.append(3)
> z.append(4)
> z.append(5)
> z.append(6)
> z.append(7)


That can be written as:

z = [0, 1, 2, 3, 4, 5, 6, 7]

Or better still:

z = range(8) # In Python 3, use list(range(8)) instead.



--
Steven

Reply With Quote
  #7 (permalink)  
Old 08-09-2012, 09:05 AM
Jean-Michel Pichavant
Guest
 
Posts: n/a
Default Re: Is there a clever way to pass arguments

bruceg113355@gmail.com wrote:
> Is there a way in Python to pass arguments without listing each argument?
> For example, my program does the following:
>
> testData (z[0], z[1], z[2], z[3], z[4], z[5], z[6], z[7])
>
> Is there a clever way to pass arguments in a single statement knowing that each argument is a sequential index from a list?
> I cannot change the function definition.
>
> Thanks,
> Bruce
>

testData(*z)

or better (imo)

testData(z) and make testData handle a list (8 parameters, that's a lot
of parameters).

JM

Reply With Quote
  #8 (permalink)  
Old 08-09-2012, 09:13 AM
Chris Angelico
Guest
 
Posts: n/a
Default Re: Is there a clever way to pass arguments

On Thu, Aug 9, 2012 at 7:05 PM, Jean-Michel Pichavant
<jeanmichel@sequans.com> wrote:
> bruceg113355@gmail.com wrote:
>>
>> I cannot change the function definition.

>
> or better (imo)
> testData(z) and make testData handle a list (8 parameters, that's a lot of
> parameters).


He can't change the function definition.

ChrisA
Reply With Quote
  #9 (permalink)  
Old 08-09-2012, 09:50 AM
Jean-Michel Pichavant
Guest
 
Posts: n/a
Default Re: Is there a clever way to pass arguments

Chris Angelico wrote:
> On Thu, Aug 9, 2012 at 7:05 PM, Jean-Michel Pichavant
> <jeanmichel@sequans.com> wrote:
>
>> bruceg113355@gmail.com wrote:
>>
>>> I cannot change the function definition.
>>>

>> or better (imo)
>> testData(z) and make testData handle a list (8 parameters, that's a lot of
>> parameters).
>>

>
> He can't change the function definition.
>
> ChrisA
>

my bad

JM
Reply With Quote
  #10 (permalink)  
Old 08-09-2012, 07:39 PM
GangGreene
Guest
 
Posts: n/a
Default Re: Is there a clever way to pass arguments

Alister wrote:

[putolin]

> some people read these threads to learn general concepts & not to find
> answers to a single explicit case.


Some people (me) don't know the first thing about python and are in the
learning/exploratory phase.

Reply With Quote
  #11 (permalink)  
Old 08-09-2012, 07:48 PM
Terry Reedy
Guest
 
Posts: n/a
Default Re: Is there a clever way to pass arguments

On 8/9/2012 5:50 AM, Jean-Michel Pichavant wrote:
> Chris Angelico wrote:
>> On Thu, Aug 9, 2012 at 7:05 PM, Jean-Michel Pichavant
>> <jeanmichel@sequans.com> wrote:
>>> bruceg113355@gmail.com wrote:
>>>> I cannot change the function definition.
>>> or better (imo)
>>> testData(z) and make testData handle a list (8 parameters, that's a
>>> lot of
>>> parameters).

>>
>> He can't change the function definition.


One can always wrap a function with an adaptor if the signature is too
awful.

--
Terry Jan Reedy

Reply With Quote
 
Reply

Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 10:10 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.