what is lambda used for in real code?

Roy Smith roy at panix.com
Sat Jan 1 15:27:30 CET 2005


In article <mailman.18.1104531731.22381.python-list at python.org>,
 Adam DePrince <adam at cognitcorp.com> wrote:

> We can operate on every other class without having to involve the
> namespace, why should functions be any different?

def is a weird beast.  It does more than just bind a lambda to a name, 
it also alters the function so it knows its own name.  For example, the 
following:

--------------
def foo ():
    print "I am foo"

bar = foo

def foo ():
    print "I am foo's evil twin"

foo()
print foo

bar()
print bar

baz = lambda : "I am lambda"
print baz
print baz()
--------------

will print:

I am foo's evil twin
<function foo at 0x363c70>
I am foo
<function foo at 0x35ae70>
<function <lambda> at 0x363770>
I am lambda



More information about the Python-list mailing list