Lambda as declarative idiom (was RE: what is lambda used for in real code?)

Robert Brewer fumanchu at amor.org
Fri Dec 31 22:58:07 EST 2004


Steven Bethard wrote:
> * Rewritable with existing functions
> Mainly these are examples of code that can benefit from using the
> functions available in the operator module, especially 
> operator.itemgetter and operator.attrgetter (available in 2.4)
> ...
> * Rewritable with list comprehensions/generator expressions
> Lambdas in map or filter expressions can often be replaced by an
> appropriate list comprehension or generator expression (in Python 2.3/2.4)
> ...
> So, those are my thoughts on how lambdas are "really" used.
> If others out there have real-life code that uses lambdas
> in interesting ways, feel free to share them here!

Admittedly, I use lambdas in a unique way; I turn them into SQL statements. But it points out what lambdas could become: a generic declarative idiom. You might notice that operator.add, for example, is not optimized for ints and strings like BINARY_ADD is (have a gander at ceval.c); instead, operator.add uses the "slow" PyNumber_Add. But your rewrites using operator.add are much faster than the equivalent lambdas, most likely due to the function-call overhead.

Wouldn't it be great if we could eval a lambda once, optimizing it as much as possible (using techniques like attrgetter, and possibly type annotations), and then let it run over large data sets? In the case of "lambda x: x + 3", we could use a type annotation to restrict the values of x to ints, and then take advantage of a compiler-side optimized int + int, evaluated once, rather than on each call. Lambdas would be much easier to optimize than full-blown functions (especially since most operations within lambdas do not produce side-effects).

I know; pipe dream. But that's what *I* use lambdas for (even if I have to transform Python bytecode into another language to do it at the moment ;).


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org





More information about the Python-list mailing list