what is lambda?

david_ullrich at my-deja.com david_ullrich at my-deja.com
Wed Jun 28 12:53:28 EDT 2000


In article <8jd8uu$gnh$1 at nnrp1.deja.com>,
  kpmurphy at my-deja.com wrote:
> 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,

   I actually think it's covered in the docs. What it does
is allow you to construct "anonymous functions".

   Oops, not my own words. The syntax

lambda x: [expression in x]

is itself an expression - the _value_ of the expression
"lambda x: [expression in x]" is a function which returns
[expression in x] when passed x. For example "lambda x: x+x"
is a function that returns x+x; saying

f = lambda x: x+x

has the same effect as saying

def f(x):
  return x + x

   But you don't usually use it that way - people use
lambda when they want to pass a function to some
routine without having to make up a name for the
function. For example map takes a function as a
parameter; saying

print map(lambda x: x+x, [1,2,3])

is the same as saying

def f(x):
  return x + x

print map(f, [1,2,3]])

   You should note there's varying opinions on whether
lambda is a good thing - if you're a beginner at programming
as well as with Python it's not the first thing you should
worry about.

DU

> Keith
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list