Make delicious recipes!

Lambda expressions



Lambda expressions in Python are somewhat like an anonymous function.

>>> lmd = lambda x:x+2
>>> lmd (2)
4


# lambda expressions can take more than one argument   
>>> lmd = lambda x,y: (x+y)
>>> lmd (2,3)
5


# lambda expressions can use other lambda expressions   
>>> lmd = lambda x:x+2
>>> lmd2 = lambda x,y: lmd(x) + lmd(y)
>>> lmd2 (2,3)
9


# lambda expressions can be returned as values from functions 
# Each call to the function returns a different lambda function    
# (something like closure in JavaScript)
>>> def func_maker (n): return lambda x: x+n
>>> lmd1 = func_maker(1)
>>> lmd2 = func_maker(2)

>>> lmd1(3)
4
>>> lmd2(3)
5













Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal