[Python-Dev] [rfc] map enhancement

Guido van Rossum guido@python.org
Tue, 18 Feb 2003 08:00:02 -0500


> Using map you always end up using lambda functions to pass constant
> arguments to the mapping function like this:
> 
> map( lambda x,y=K: f(x,y), L )

This can now done by writing instead:

  map(lambda x: f(x, K), L)

or if you don't want the lambda, you can use a list comprehension:

  [f(x, K) for x in L]

> What I propose in this patch is to have map recognize its keyword
> arguments like this:
> 
> map( f, L , y=K )
> 
> The result would be:
> def map( f, L, **kw ): # accepts only one list for simplicity
>    l=[]
>    for i in L:
>       l.append( f(i,**kw) )
>    return l
> 
> While it doesn't solve every problem (passing a value to the first arg)
> it could help remove many uses of lambda functions.

While your patch scores high in minimal changes to the implementation,
I don't like the resulting notation.  While map(f, L) is reasonably
natural, map(f, L, y=K) is not.  Also, most C functions don't accept
keyword arguments, so this won't help as much in the case where
there's a speed reason for avoiding lambdas and list comprehensions.

--Guido van Rossum (home page: http://www.python.org/~guido/)