what is lambda used for in real code?

Alex Martelli aleaxit at yahoo.com
Fri Dec 31 19:08:47 EST 2004


Steven Bethard <steven.bethard at gmail.com> wrote:
   ...
> > Your reasoning makes sense... lambda enables you to create a function as
> > part of an expression, just like other types can be part of an 
> > expression.  However, by that same reasoning, maybe classes aren't 
> > special enough either to warrant a special case.  Where's the keyword to
> > create an anonymous class? :-)
> 
> Well, no keyword, but you can use the type function:
> 
> py> d = dict(c=type('C', (object,), dict(spam=42)),
> ...          d=type('D', (dict,), dict(badger=True)))
> py> d['c'].spam
> 42
> py> d['c']()
> <__main__.C object at 0x063F2DD0>

Well then, just call new.function to similarly create functions as part
of an expression, hm?  Passing the bytecode in as a string isn't
incredibly legible, OK, but, we've seen worse...;-)


Alex



More information about the Python-list mailing list