Can someone give me a short explanation?

Daniel Dittmar daniel.dittmar at sap.com
Mon Apr 5 05:30:19 EDT 2004


Senthoorkumaran Punniamoorthy wrote:
> I found this code in the book text processing with Python. But having
> little difficulty understanding how exactly it works and what should
> be passed as argument? Can someone decode this for me please?
>
> apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns))

def apply_each (fns, args = []):
    return map (apply, fns, [args]*len(fns))

map(...)
    map(function, sequence[, sequence, ...]) -> list

    Return a list of the results of applying the function to the items of
    the argument sequence(s).  If more than one sequence is given, the
    function is called with an argument list consisting of the corresponding
    item of each sequence, substituting None for missing values when not all
    sequences have the same length.  If the function is None, return a list
of
    the items of the sequence (or a list of tuples if more than one
sequence).

so apply_each ([func1, func2, func3], [arg1, arg2])
is the same as
[func1 (arg1, arg2), func2 (arg1, arg2), func3 (arg1, arg)]

or using list comprehension
[func (arg1, arg2) for func in [func1, func2, func3])

Daniel






More information about the Python-list mailing list