creating multiply arguments for a method.

N

noro

Hi all,

I use a method that accept multiply arguments ("plot(*args)").
so plot([1,2,3,4]) is accepted and plot([1,2,3,4],[5,6,7,8]) is also
accepted.

the problem is that i know the number of arguments only at runtime.
Let say that during runtime i need to pass 4 arguments, each is a list,
creating a set of lists and passing it to the method wont word since
the interpartor thinks it is only 1 argument the contain a reference to
a "list of lists", instede of number of arguments, each is a list.

any suggestions?
thanks
amit
 
F

Fredrik Lundh

noro said:
Let say that during runtime i need to pass 4 arguments, each is a list,
creating a set of lists and passing it to the method wont word since
the interpartor thinks it is only 1 argument the contain a reference to
a "list of lists", instede of number of arguments, each is a list.
.... print "got", len(args), "argument(s)"
....
>>> args = [[1, 2, 3, 4], [5, 6, 7, 8]]
>>> func(args) got 1 arguments
>>> func(*args)
got 2 arguments

</F>
 
J

John Roth

noro said:
Hi all,

I use a method that accept multiply arguments ("plot(*args)").
so plot([1,2,3,4]) is accepted and plot([1,2,3,4],[5,6,7,8]) is also
accepted.

the problem is that i know the number of arguments only at runtime.
Let say that during runtime i need to pass 4 arguments, each is a list,
creating a set of lists and passing it to the method wont word since
the interpartor thinks it is only 1 argument the contain a reference to
a "list of lists", instede of number of arguments, each is a list.

any suggestions?
thanks
amit

Why do you want to do this? You'll have to do some
logic in your method body to determine how many
operands you have whether you explicitly pass a list or
whether you have the system break it apart into
separate parameters.

Fredrick Lund's solution, using an * parameter in the
method definition, will produce a list that you have to
pull apart in the method. Doing the same in the method
call takes a single list of all of your parameters and then
distributes it among the parameters in the definition.

I wouldn't bother with either one. Passing a list of my
real parameters as a single parameter is, in most
circumstances, easier and IMO clearer.

John Roth
 
N

noro

John said:
noro said:
Hi all,

I use a method that accept multiply arguments ("plot(*args)").
so plot([1,2,3,4]) is accepted and plot([1,2,3,4],[5,6,7,8]) is also
accepted.

the problem is that i know the number of arguments only at runtime.
Let say that during runtime i need to pass 4 arguments, each is a list,
creating a set of lists and passing it to the method wont word since
the interpartor thinks it is only 1 argument the contain a reference to
a "list of lists", instede of number of arguments, each is a list.

any suggestions?
thanks
amit

Why do you want to do this? You'll have to do some
logic in your method body to determine how many
operands you have whether you explicitly pass a list or
whether you have the system break it apart into
separate parameters.

Fredrick Lund's solution, using an * parameter in the
method definition, will produce a list that you have to
pull apart in the method. Doing the same in the method
call takes a single list of all of your parameters and then
distributes it among the parameters in the definition.

I wouldn't bother with either one. Passing a list of my
real parameters as a single parameter is, in most
circumstances, easier and IMO clearer.

John Roth

I do not have much choise. I did not wirte the module, i just use it.

thank you all for the help,
amit
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top