"unlist" a list

Joonas Paalasmaa joonas at olen.to
Fri Jan 18 04:24:08 EST 2002


pekka niiranen wrote:
> 
> How about feeding function a list of parameters (para) ?
> 
> l1 = ['v1', 'v2']
>  >>> l2 = ['op1','op2','op3']
>  >>> para = l1 + l2
>  >>> para
> ['v1', 'v2', 'op1', 'op2', 'op3']

Built-in function apply can be used to feed a list of parameters to a
function.

>>> def func(first, second, third):
	print "Got", first, second, third

>>> func(1,2,3)
Got 1 2 3
>>> opts = [1,2]
>>> apply(func, [0]+opts)
Got 0 1 2


Help on built-in function apply:

apply(...)
    apply(object[, args[, kwargs]]) -> value
    
    Call a callable object with positional arguments taken from the
tuple args,
    and keyword arguments taken from the optional dictionary kwargs.
    Note that classes are callable, as are instances with a __call__()
method.



> -pekka-
> 
> Klaus Hoeppner wrote:
> 
> >Hi,
> >maybe a silly question:
> >
> >I use a funktion that is defined as foo(v1,v2,*args), i.e. it is
> >called as
> >  a = foo(v1,v2,opt1,opt2,opt3...)
> >(in fact in my case foo is Tkinter.OptionMenu)
> >
> >Unfortunately, I have the optional arguments in list
> >  options = [opt1,opt2,opt3,...]
> >and oviously foo(v1,v2,options) goes wrong since foo assumes that
> >opt1 is the list options.
> >So I would need something like a unlist-operator telling foo that
> >the list is not the single first optional arguments but the members
> >of the list are the optional arguments.
> >
> >Any idea?
> >
> >Regards
> >Klaus
> >



More information about the Python-list mailing list