[Tutor] Lambda

Andy W toodles@yifan.net
Tue, 5 Feb 2002 14:43:53 +0800


> I not only find them counterintuitive, but can't even figure out what they
do in a line of code.  I would be grateful if someone could translate a line
using lambda    > into one that doesn't.

Really all lambda does is create an unnamed function. So now you can define
a function using "def" or by using lambda.
Say you want a simple function:

def add(x,y):
    return x+y

This can be done using a lambda as follows:

lambda x,y: x+y

Now we have a basic function object that can be assigned to a name.
The lambda syntax is "lambda arg1,arg2,...argn: expression", expression will
be evaluated and returned from the function.
So just to go over the example again, we now can do:

add = lambda x,y: x+y

add =
lambda x,y:                                      x+y
^^
^^                                       ^^
variable name which will
arguments to pass in             the expression to be evaluated and returned
now refer to the function

I hope this clears up lambdas for you, if even only a little bit.
Andy

----- Original Message -----
From: Greg Guest
To: tutor@python.org
Sent: Tuesday, February 05, 2002 10:28 AM
Subject: [Tutor] Lambda


Hi everyone,

Emboldened by Jeff Shannon's remark of a couple of weeks ago that he finds
lambdas counterintuitive, I ask my first question to the tutor.

I not only find them counterintuitive, but can't even figure out what they
do in a line of code.  I would be grateful if someone could translate a line
using lambda into one that doesn't.

I've been reading the tutor for a couple of months now, and find it a
wonderful place.  The threads on Classes have helped me a lot.

TIA
Greg Guest

A Palindrome:
Straw? No, too stupid a fad.  I put soot on warts.



____________________________________________________________
Get Premium UNLIMITED Internet Access across Canada for
JUST $9.95/MONTH at http://www.get3web.com/?mkid=emt123