[Tutor] lambda

Adam Bark adam.jtm30 at gmail.com
Sat Mar 19 16:06:27 CET 2011


On 19/03/11 14:44, 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

Here's one from the python docs, reduce takes a function so technically 
the programmer only uses it once but the program calls it many times. 
Map is another that works similarly. Have a look into functional 
programming as that's the branch of computer science those two functions 
and lambdas tend to come from I believe.
http://docs.python.org/library/functions.html?highlight=lambda#reduce

HTH,
Adam.


More information about the Tutor mailing list