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 [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list