Prepending / appending to *args

Thomas Wouters thomas at xs4all.net
Sun Jul 9 15:34:50 EDT 2000


On Sun, Jul 09, 2000 at 09:21:12PM +0200, Nikolai Kirsebom wrote:

> I think I've seen how this is done somewhere, but cannot find it.  Could
> anyone show me how I in function A below add an argument at the beginning of
> the list / at the end of the list which then is supplied to function B.

> def A(*args):
>   pa = 'prepended-argument'
>   aa = 'appended-argument'
>   B(pa+args+aa)   ''''SYNTAX here ??

  apply(B, pa+args+aa)

However, 'args' is not a list! It's a tuple. There is a difference,
especially in how you append or prepend items. In the example above, you're
adding strings and tuples, and that won't work. Try it like this:

  apply(B, (pa,) + args + (aa,))

In Python 1.6 (the alphas, and the betas and final, if they ever arrive ;-)
and Python 2.0 (aka the CVS tree ;) you can use the '*syntax' when calling a
function too:

  newargs = (pa,) + args + (aa,)
  B(*newargs)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list