Detupleize a tuple for argument list

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Jul 5 08:30:46 EDT 2006


On Wed, 05 Jul 2006 14:01:27 +0200, Marco Wahl wrote:

> Hi,
> 
> I want to give a tuple to a function where the function
> expects the respective tuple-size number of arguments.
> 
> The following session illustrates what I want to do and
> the respective failure.
> 
>  Python 2.4.1 (#7, Aug  3 2005, 14:55:58) 
>  [GCC 3.3.1 (SuSE Linux)] on linux2
>  Type "help", "copyright", "credits" or "license" for more information.
>  >>> def foo(a, b): print a, b
>  ... 
>  >>> t = (1, 2)
>  >>> def foo(a, b): print 'a == %s, b == %s' % (str(a), str(b))
>  ... 
>  >>> foo(1, 2)
>  a == 1, b == 2
>  >>> foo(t)
>  Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
>  TypeError: foo() takes exactly 2 arguments (1 given)

Easy:  foo(*t)

-- 
Steve.




More information about the Python-list mailing list