what is lambda used for in real code?

Steven Bethard steven.bethard at gmail.com
Fri Dec 31 17:36:25 EST 2004


Adam DePrince wrote:
> Lets not forget the "real reason" for lambda ... the elegance of
> orthogonality.   Why treat functions differently than any other object? 
> 
> We can operate on every other class without having to involve the
> namespace, why should functions be any different?

Yup.  I think in most of the examples that I didn't know how to rewrite, 
this was basically the issue.  On the other hand, I do think that 
lambdas get overused, as indicated by the number of examples I *was* 
able to rewrite.[1]

Still, I have to admit that in some cases (especially those involving 
reduce), I wish the coder had named the function -- it would have given 
me a little bit more documentation as to what the code was trying to do.

On the other hand, in other cases, like when a function is a keyword 
argument to another function (e.g. inspect.py's "def formatargspec..." 
example) using a def statement and naming the function would be redundant.

Steve

[1] Note that this isn't entirely fair to the examples, some of which 
were written before list comprehensions, generator expressions and 
itemgetter/attrgetter.



More information about the Python-list mailing list