[Tutor] map, filter and lambda functions

alan.gauld@bt.com alan.gauld@bt.com
Thu, 23 Aug 2001 17:17:56 +0100


> > You could define functions:
> > def stripitem(x): return string.strip(x)
> > def upperitem(x): return string.upper(x)
> 
> Same for the last two here. You might as well write
> 
> stripitem = string.upper

Sure, I was just making the point that in general

foo = lambda x: <expr>

can be replaced by

def foo(x): return <expr>
 
THe case where <expr> is a standard function just 
happens to be a special case where the def is done for you.

> > You can always replace a lambda with a function in Python, 
> > but in this case the lambda is probably clearer by putting 
> > the action explicitly in the operation.
> 
> Except that the lambda does no action it all, so it's obfuscating :)

Its still less obfuscating than passing a user defined function might be.
Despite the friendly name 'upperitem' we don't really know whether its 
a wrapper for string.upper or  math.ceil or something we wrote ourselves. 
At least the lambda in this case exposes the function used. Which, again, 
was the gerneral point I was trying to make about the use of lambdas.

Alan G