En Fri, 07 Sep 2007 00:02:12 -0300, Ben Warren <[EMAIL PROTECTED]> escribi�:
> Hello, > > Let's say I have a function with a variable number of arguments (please > ignore syntax errors): > > def myfunc(a,b,c,d,...): > > and I have a tuple whose contents I want to pass to the function. The > number of elements in the tuple will not always be the same. > > T = A,B,C,D,... > > Is there a way that I can pass the contents of the tuple to the function > without explicitly indexing the elements? Something like: > > myfunc(magic(T)) Use *T when calling the function: py> def myfunc(a, b, c, d=None, e=None): ... print a,b,c,d,e ... py> T = 1,2,3,4,5 py> myfunc(*T) 1 2 3 4 5 py> T = 1,2,3 py> myfunc(*T) 1 2 3 None None py> T = 1,2 py> myfunc(*T) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: myfunc() takes at least 3 arguments (2 given) A similar syntax works when defining a function: *args will receive all remaining positional arguments joined into a tuple: py> def otherfunc(a, b, *args): ... print a, b, args ... py> otherfunc(1,2,3,4,5) 1 2 (3, 4, 5) py> T = 1,2,3,4 py> otherfunc(*T) 1 2 (3, 4) py> otherfunc('a','b','c',*T) a b ('c', 1, 2, 3, 4) This is covered in the Python Tutorial at <http://docs.python.org/tut/node6.html#SECTION006700000000000000000> and you can read the nasty details here <http://docs.python.org/ref/calls.html> -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list