[Tutor] lambda

Dave Angel davea at ieee.org
Sat Mar 19 19:20:20 CET 2011

On 01/-10/-28163 02:59 PM, Ajit Deshpande wrote:
> I am trying to figure out where lambda functions can be useful. Has anyone
> used them in real world?
>> From my reading so far, I hear people claim that lambda can be a useful
> replacement for small functions. Most examples  didn't make much sense to
> me. Why would anyone use a one liner anonymous function, if you never plan
> to use it elsewhere? You would then be implementing the logic directly in
> the line, isn't it?. Functions are useful if they plan to get called
> multiple times within your code.
> For example:
> add_one = lambda x: x + 1
> In real world, why would I use a lambda for this. I would simply do:
> add_one = x + 1
> Can you provide some useful use cases for lambda functions?
> ~ Ajit Deshpande

The other comments are useful.  Lambdas are usually used for callback 
functions.  But I wanted to point out that your two add_one objects are 
not at all equivalent.  The first one is a function, which can be called 
at a later time.  The second one is probably an integer, depending of 
course on the type of x.

To get the equivalent of the lambda function above, you'd use

def add_one(x):
     return x+1


More information about the Tutor mailing list