Is there a clever way to pass arguments

B

bruceg113355

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
 
A

Andrew Cooper

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
 
D

Dave Angel

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.
 
B

bruceg113355

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.


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
 
B

bruceg113355

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.


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
 
S

Steven D'Aprano

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.
 
J

Jean-Michel Pichavant

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
 
C

Chris Angelico

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
 
G

GangGreene

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top