forwarding *arg parameter

Stargaming stargaming at gmail.com
Sun Nov 5 11:28:05 EST 2006


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
> 
> TV
Use the following then:
 >>> g(*g('foo', 'bar'))
('foo', 'bar')

Otherwise, you would have to check if arg is a 1-tuple consisting of a 
tuple and "strip" it out then.

e.g.
 >>> def g(*arg):
...  return arg[0] if isinstance(arg[0], tuple) else arg
...
 >>> g('foo', 'bar')
('foo', 'bar')
 >>> g(g('foo', 'bar'))
('foo', 'bar')



More information about the Python-list mailing list