And if you're interested, the name "lambda" itself comes from lambda 
calculus:
 
http://en.wikipedia.org/wiki/Lambda_calculus
http://en.wikipedia.org/wiki/Lambda_calculus#First-class_functions
http://en.wikipedia.org/wiki/Anonymous_functions
 

On Tuesday, December 21, 2010 4:02:36 PM UTC-5, stefaan wrote:

> pbreit wrote:
> > I see lambda used quite a bit and don't totally understand the concept.
> > Is there a simple rule to follow to know when it is necessary to use? 
>
> Sometimes you need to pass a function as an argument to another 
> function. In that case you have a choice to 
>
> 1. define a separate function, and pass that to the function you are 
> calling: 
>
> def my_fun(x, y, operation):
> return operation(x, y) 
>
> The function my_fun takes two numbers x and y, and one function 
> "operation" that is applied to x and y 
>
> You can e.g. use it as follows to perform an addition: 
>
> def my_addition(x, y):
> return x+y 
>
>  >>> print my_fun(1, 2, my_addition)
> 3 
>
> 2. you can omit defining the separate function "my_addition" and pass in 
> an anonymous function (defined using lambda) instead: 
>
> def my_fun(x, y, operation):
> return operation(x,y) 
>
>  >>> print my_fun(1,2, lambda x,y : x+y)
> 3 
>
> So lambda is used to define a function on-the-spot, without even 
> bothering to give it a name of its own (an "anonymous" function). The 
> "lambda x,y" part tells the system that you define an anonymous function 
> expecting two arguments named x and y. The x+y part tells the system 
> that the anonymous function returns the sum of its x and y arguments. 
>
> Hope this helps,
> Best regards,
> Stefaan. 
>
>       

Reply via email to