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


Groups > comp.lang.python > #26772

Re: Is there a clever way to pass arguments

Newsgroups comp.lang.python
Date 2012-08-08 18:20 -0700
References <0aaad6e4-8751-4073-bf9c-14285c1f3422@googlegroups.com> <mailman.3088.1344474457.4697.python-list@python.org>
Subject Re: Is there a clever way to pass arguments
From bruceg113355@gmail.com
Message-ID <mailman.3089.1344475242.4697.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web