what is lambda?

Warren Postma embed at NOSPAM.geocities.com
Wed Jun 28 12:46:29 EDT 2000


> I am a beginner at python and I would like to know what lambda does.
> Could someone post an in-your-own-words definition and possibly an
> example?  Thanks,

1) Meta-Answer: Read the FAQ and Manual sections.

2) Instant Gratification Answer

here's a standard function definition:

def func(x):
    return x *2

here's a way to write the same thing, in effect, but using Lambda:

func = lambda (x): x * 2

While that example isn't useful, it gives you the idea.  Think of lambda as
a way to generate a function and return that function as an object, without
having to give it a name. I primariyl use it to pass an algorithm or
expression as a parameter to a function, or other places where I want to
pass code in a variable instead of passing a reference to a method
containing that code. Clear


Warren Postma





More information about the Python-list mailing list