forwarding *arg parameter

Tuomas tuomas.vesterinen at pp.inet.fi
Sun Nov 5 11:04:54 EST 2006


Diez B. Roggisch wrote:
> Tuomas schrieb:
> 
>>  >>> def g(*arg):
>> ...     return arg
>> ...
>>  >>> g('foo', 'bar')
>> ('foo', 'bar')
>>  >>> # seems reasonable
>> ...
>>  >>> g(g('foo', 'bar'))
>> (('foo', 'bar'),)
>>  >>> # not so good, what g should return to get rid of the outer tuple
> 
> 
> g(*g('foo', 'bar'))
> 
> 
> * and **  are the symetric - they capture ellipsis arguments, and they 
> make iterables/dicts passed as positional/named arguments.
> 
> Diez

Thanks Diez

And what about this case if I want the result ('foo', 'bar')

 >>> def f(*arg):
...     return g(arg)
...
 >>> f('foo', 'bar')
(('foo', 'bar'),)

 >>> def h(*arg):
...     return arg[0]
...
 >>> g=h
 >>> f('foo', 'bar')
('foo', 'bar')

Where can g know it should use arg[0] when arg is forwarded?

TV



More information about the Python-list mailing list