what is lambda used for in real code?

Steven Bethard steven.bethard at gmail.com
Fri Dec 31 23:53:42 CET 2004


Hans Nowak wrote:
> Adam DePrince wrote:
> 
>> 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.
> 
> 
> 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>


Steve



More information about the Python-list mailing list