Anonymus functions revisited

Antoon Pardon apardon at forel.vub.ac.be
Wed Mar 23 07:07:32 EST 2005


Op 2005-03-23, Diez B. Roggisch schreef <deetsNOSPAM at web.de>:
>> Can someone who thinks this way, please explain why this is acceptable
>> 
>>   [ x * x for x in some_iterator ]
>> 
>> But this is not
>> 
>>   map(lambda x: x * x, some_iteraror)
>> 
>> and should be replaced with
>> 
>>   def sqr(x): return x * x
>>   map(sqr , some_iterator)
>
> It shouldn't, it should be replaced with the listcomp. And IMHO it results
> in better readable code. 

This misses the point. The fact that map and the listcomprehension do
the same stuff is just a detail. It is about giving a function an
expression as argument that is to be evaluated multiple times.

> AFAIK the reason people say you should use named functions is _not_ because
> of these single expression replacements. The reason is that frequently
> people complain about lambda being so restrictive so that you can't write
>
> lambda x,y: if x > 100: x else: y
>
> and want to _extend_ lambda beyond its current capabilities and make it full
> featured but anonymous functions.

This is one kind of people. An other kind of people seems to want to
get rid of lamda's altogether and asks whether it is so difficult
to make a named function each time you would otherwise use a lambda.

> But as I've said various times before: I personally don't mind lambdas and
> for example the reduce function has been useful for me quite a few times,
> can't be replaced by listcomps, and frequently needs a callable consisting
> of only a single expression. So I'll continue to use lambdas there.

Then my question was not meant for you.

-- 
Antoon Pardon



More information about the Python-list mailing list