what is lambda used for in real code?
adam at cognitcorp.com
Tue Jan 4 22:38:13 EST 2005
On Fri, 2004-12-31 at 22:09, Terry Reedy wrote:
> "Adam DePrince" <adam at cognitcorp.com> wrote in message
> news:1104531721.3724.18.camel at localhost.localdomain...
> > In sort, we must preserve the ability to create an anonymous function
> > simply because we can do so for every other object type, and functions
> > are not special enough to permit this special case.
> Please show me how to create an anonymous type, module, or class,
You can create anonymous instances of classes and types. The actual
type, module or class .. well, that is another special case that isn't
special enough ... but that is somewhat beyond the scope of this
thread. I've seen a number of responses similar to this; I feel that
the lack of anonymous classes, types and modules are flaws with the
language that should be fixed, not used to justify introducing another
It is my humble opinion that a good language design is completely
orthogonal. The curse of anonymity in the language can be "cured" at a
management/specification level by requiring the use of the name space.
The alternative, the removal of anonymous functions, adds a "special
case" and dumbs down the language. Personally, I'd like to see Python
evolve in the direction of ML instead of Visual Basic.
There are actually times when I've wanted anonymous classes. Very
often, I've written code that accepts as a parameter, a class. Of
course, the code instantiates this class with the expectation that it
provides a particular interface. And sometimes, it would have been nice
to, in a single invocation of such a function, change a single method of
the class before passing it. In a sense, I've written code for which a
class or type equivalent to a lambda would actually have been really
> especially with an expression. Number, sequences, and dicts have easily
> printable values. Functions, like classes and module do not*, so
So do lambdas, classes, types, etc. And I've encountered a good many
lists and dicts that don't qualify as "easily printable." Quite
frankly, it seems a bit capricious to *require* the use of the name
space on the basis of the similarity between the repr of the object and
And here is a counter example ... this def fits nicely on one line:
Python 2.4 (#1, Dec 27 2004, 15:19:19)
[GCC 3.4.1] on linux2
>>> def a( b ): return b+1
>>> a( 2 )
> definition names act as a standin and as a pointer to the source code that
> produced the object. If functions are not special relative to classes and
> modules, which is the grouping they belong with, then we should get rid of
> lambda ;-)
> *Now that memory is 'cheap', someone recently proposed that code objects
> (and hence functions) get .source attribute. Maybe, someday...
The .source attribute could be free from a memory consumption
perspective if the source -> byte code conversion is reversible.
> Terry J. Reedy
More information about the Python-list