Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26768 > unrolled thread

Is there a clever way to pass arguments

Started bybruceg113355@gmail.com
First post2012-08-08 17:41 -0700
Last post2012-08-09 15:48 -0400
Articles 12 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  Is there a clever way to pass arguments bruceg113355@gmail.com - 2012-08-08 17:41 -0700
    Re: Is there a clever way to pass arguments Andrew Cooper <amc96@cam.ac.uk> - 2012-08-09 01:47 +0100
    Re: Is there a clever way to pass arguments Dave Angel <d@davea.name> - 2012-08-08 21:07 -0400
      Re: Is there a clever way to pass arguments bruceg113355@gmail.com - 2012-08-08 18:20 -0700
      Re: Is there a clever way to pass arguments bruceg113355@gmail.com - 2012-08-08 18:20 -0700
        Re: Is there a clever way to pass arguments Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-09 01:34 +0000
    Re: Is there a clever way to pass arguments Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-08-09 11:05 +0200
    Re: Is there a clever way to pass arguments Chris Angelico <rosuav@gmail.com> - 2012-08-09 19:13 +1000
      Re: Is there a clever way to pass arguments Alister <alister.ware@ntlworld.com> - 2012-08-09 17:14 +0000
        Re: Is there a clever way to pass arguments GangGreene <GangGreene@invalid.com> - 2012-08-09 15:39 -0400
    Re: Is there a clever way to pass arguments Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-08-09 11:50 +0200
    Re: Is there a clever way to pass arguments Terry Reedy <tjreedy@udel.edu> - 2012-08-09 15:48 -0400

#26768 — Is there a clever way to pass arguments

Frombruceg113355@gmail.com
Date2012-08-08 17:41 -0700
SubjectIs there a clever way to pass arguments
Message-ID<0aaad6e4-8751-4073-bf9c-14285c1f3422@googlegroups.com>
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

[toc] | [next] | [standalone]


#26769

FromAndrew Cooper <amc96@cam.ac.uk>
Date2012-08-09 01:47 +0100
Message-ID<FMDUr.845824$ig.360719@fx24.am4>
In reply to#26768
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/controlflow.html#more-on-defining-functions

~Andrew

[toc] | [prev] | [next] | [standalone]


#26770

FromDave Angel <d@davea.name>
Date2012-08-08 21:07 -0400
Message-ID<mailman.3088.1344474457.4697.python-list@python.org>
In reply to#26768
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

[toc] | [prev] | [next] | [standalone]


#26771

Frombruceg113355@gmail.com
Date2012-08-08 18:20 -0700
Message-ID<8a815fe6-4f2f-438e-84ac-04063eb592ba@googlegroups.com>
In reply to#26770
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

[toc] | [prev] | [next] | [standalone]


#26772

Frombruceg113355@gmail.com
Date2012-08-08 18:20 -0700
Message-ID<mailman.3089.1344475242.4697.python-list@python.org>
In reply to#26770
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

[toc] | [prev] | [next] | [standalone]


#26774

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-08-09 01:34 +0000
Message-ID<502313a7$0$29978$c3e8da3$5496439d@news.astraweb.com>
In reply to#26772
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

[toc] | [prev] | [next] | [standalone]


#26782

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2012-08-09 11:05 +0200
Message-ID<mailman.3096.1344503139.4697.python-list@python.org>
In reply to#26768
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

[toc] | [prev] | [next] | [standalone]


#26783

FromChris Angelico <rosuav@gmail.com>
Date2012-08-09 19:13 +1000
Message-ID<mailman.3097.1344503614.4697.python-list@python.org>
In reply to#26768
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

[toc] | [prev] | [next] | [standalone]


#26791

FromAlister <alister.ware@ntlworld.com>
Date2012-08-09 17:14 +0000
Message-ID<RdSUr.1028637$LY3.103962@fx22.am4>
In reply to#26783
On Thu, 09 Aug 2012 19:13:31 +1000, 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

True but it is still worth highlighting that the function definition may 
not be ideal depending on the requirements of the function. 
some people read these threads to learn general concepts & not to find 
answers to a single explicit case.



-- 
I want to so HAPPY, the VEINS in my neck STAND OUT!!

[toc] | [prev] | [next] | [standalone]


#26801

FromGangGreene <GangGreene@invalid.com>
Date2012-08-09 15:39 -0400
Message-ID<m8gdf9-456.ln1@crazy-horse.bildanet.com>
In reply to#26791
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. 

[toc] | [prev] | [next] | [standalone]


#26784

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2012-08-09 11:50 +0200
Message-ID<mailman.3098.1344505847.4697.python-list@python.org>
In reply to#26768
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

[toc] | [prev] | [next] | [standalone]


#26804

FromTerry Reedy <tjreedy@udel.edu>
Date2012-08-09 15:48 -0400
Message-ID<mailman.3109.1344541818.4697.python-list@python.org>
In reply to#26768
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web