Flatten a list/tuple and Call a function with tuples

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Wed Jul 25 21:21:22 EDT 2007


On Wed, 25 Jul 2007 15:46:58 +0000, beginner wrote:

> I know the * operator. However, a 'partial unpack' does not seem to
> work.
> 
> def g():
>   return (1,2)
> 
> def f(a,b,c):
>   return a+b+c
> 
> f(*g(),10) will return an error.


No it doesn't, it _raises_ an exception. This is a function that returns
an error:

def function():
    """Returns an error."""
    return Error()  # defined elsewhere

But to answer your question:

> Do you know how to get that to work?

It's a little bit messy, but this works:

>>> f(*(g() + (10,)))
13

This is probably easier to read:

>>> t = g() + (10,); f(*t)
13


-- 
Steven.




More information about the Python-list mailing list