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.

Community
  • 1
  • 1
user1401321
  • 111
  • 12

1 Answers1

3

try this,,

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)

Even for calling * will help you...

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)
avasal
  • 14,350
  • 4
  • 31
  • 47
  • Thank you! That is close, but not quite what I need (I probably wasn't clear enough in my original question). 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. – user1401321 Jul 18 '12 at 09:33
  • can you be more clear,, it will help you to get appropriate answer – avasal Jul 18 '12 at 09:35
  • The answer remains: Call `client.service.method(*mylist)`. My upvote for avasal's answer was before his edit when I thought he meant the calling syntax, not the defining syntax for multiple-argument functions. But they are equivalent. – Tim Pietzcker Jul 18 '12 at 09:36