1

Possible Duplicate:
Having more then one parameter with def in python

I need to make a function call which will contain a variable number of parameters - The parameters are contained in a list.

It would "look" like this for a list of 2 items:

TheFunction(item1, item2)

And for a list of 3 items:

TheFunction(item1, item2, item3)

Is there any easy, pythonic way of doing this? Some kind of loop using getattr(), for instance?

The list is of a variable size - I need to be able to call the function with just the list presented as a argument in the code. Specifically, I am calling the client.service.method() function in SUDS to call a web service method. This function accepts a variable number of arguments, I'm simply having trouble with calling it with a variable number of arguments.

4

1 回答 1

3

尝试这个,,

TheFunction(*N_items)

In [88]: def TheFunction(*N_items):
   ....:     print N_items
   ....:

In [89]: TheFunction(1)
(1,)

In [90]: TheFunction(1,2,3)
(1, 2, 3)

In [91]: TheFunction(1,2,3,4,5)
(1, 2, 3, 4, 5)

即使打电话*也会帮助你...

In [92]: l = [1,2,3,4,5]

In [93]: TheFunction(*l)
(1, 2, 3, 4, 5)

In [94]: l = [1,2]

In [95]: TheFunction(*l)
(1, 2)
于 2012-07-18T09:29:34.683 回答