Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Is there a clever way to pass arguments

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


bruceg113355 at gmail

Aug 8, 2012, 5:41 PM

Post #1 of 12 (590 views)
Permalink
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
--
http://mail.python.org/mailman/listinfo/python-list


amc96 at cam

Aug 8, 2012, 5:47 PM

Post #2 of 12 (590 views)
Permalink
Re: Is there a clever way to pass arguments [In reply to]

On 09/08/2012 01:41, bruceg113355 [at] gmail 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
--
http://mail.python.org/mailman/listinfo/python-list


d at davea

Aug 8, 2012, 6:07 PM

Post #3 of 12 (587 views)
Permalink
Re: Is there a clever way to pass arguments [In reply to]

On 08/08/2012 08:41 PM, bruceg113355 [at] gmail 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

--
http://mail.python.org/mailman/listinfo/python-list


bruceg113355 at gmail

Aug 8, 2012, 6:20 PM

Post #4 of 12 (582 views)
Permalink
Re: Is there a clever way to pass arguments [In reply to]

On Wednesday, August 8, 2012 9:07:04 PM UTC-4, Dave Angel wrote:
> On 08/08/2012 08:41 PM, bruceg113355 [at] gmail 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
--
http://mail.python.org/mailman/listinfo/python-list


bruceg113355 at gmail

Aug 8, 2012, 6:20 PM

Post #5 of 12 (583 views)
Permalink
Re: Is there a clever way to pass arguments [In reply to]

On Wednesday, August 8, 2012 9:07:04 PM UTC-4, Dave Angel wrote:
> On 08/08/2012 08:41 PM, bruceg113355 [at] gmail 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
--
http://mail.python.org/mailman/listinfo/python-list


steve+comp.lang.python at pearwood

Aug 8, 2012, 6:34 PM

Post #6 of 12 (586 views)
Permalink
Re: Is there a clever way to pass arguments [In reply to]

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

--
http://mail.python.org/mailman/listinfo/python-list


jeanmichel at sequans

Aug 9, 2012, 2:05 AM

Post #7 of 12 (577 views)
Permalink
Re: Is there a clever way to pass arguments [In reply to]

bruceg113355 [at] gmail 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

--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Aug 9, 2012, 2:13 AM

Post #8 of 12 (576 views)
Permalink
Re: Is there a clever way to pass arguments [In reply to]

On Thu, Aug 9, 2012 at 7:05 PM, Jean-Michel Pichavant
<jeanmichel [at] sequans> wrote:
> bruceg113355 [at] gmail 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
--
http://mail.python.org/mailman/listinfo/python-list


jeanmichel at sequans

Aug 9, 2012, 2:50 AM

Post #9 of 12 (580 views)
Permalink
Re: Is there a clever way to pass arguments [In reply to]

Chris Angelico wrote:
> On Thu, Aug 9, 2012 at 7:05 PM, Jean-Michel Pichavant
> <jeanmichel [at] sequans> wrote:
>
>> bruceg113355 [at] gmail 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
--
http://mail.python.org/mailman/listinfo/python-list


alister.ware at ntlworld

Aug 9, 2012, 10:14 AM

Post #10 of 12 (576 views)
Permalink
Re: Is there a clever way to pass arguments [In reply to]

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 [at] sequans> wrote:
>> bruceg113355 [at] gmail 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!!
--
http://mail.python.org/mailman/listinfo/python-list


GangGreene at invalid

Aug 9, 2012, 12:39 PM

Post #11 of 12 (576 views)
Permalink
Re: Is there a clever way to pass arguments [In reply to]

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.

--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Aug 9, 2012, 12:48 PM

Post #12 of 12 (575 views)
Permalink
Re: Is there a clever way to pass arguments [In reply to]

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 [at] sequans> wrote:
>>> bruceg113355 [at] gmail 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

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.