While digging into lambda
I defined this code below:
def f2(number, lambda_function):
return lambda_function(number)
def f1(number):
return f2(number, lambda x: x*2)
number = 2
print f1(number)
While I do agree the code like this looks pretty cool I wonder why wouldn't I just put it down using a more traditional approach, like so:
def f1(number):
return number*2
number = 2
print f1(number)
I understand that some languages rely on the functional programming more than Python. But in Python I end up with less lines and more readable friendly code if I just avoid lambda
and the function programming tricks. Can the code above be modified to illustrate a situation when a task could not be completed without using lambda
? What is the purpose of lambda
in Python? Can you show me the example of where lambda
really "shines"? May be an example where the use of lambda
simplified the code?