2

I am reading the Python documentation, and learning python. I am on chapter 4.7.5. Lambda Expressions. I am having a tough time understanding how lambda functions are any different than any other function I might write.

Can someone break down what lambda functions are in python for me? Like into newb laymens-terms?

1
  • 4
    You'll probably get better answers for this on Stack Overflow as that is a programming-centered Stack Exchange. Raspberry Pi Stack Exchange is more focused on Raspberry Pi-specific issues and so this question is off-topic here. – RPiAwesomeness Aug 25 '14 at 19:13
3

Lambda is an anonymous function, which is often passed as an argument to regular functions. You typically use lambda expressions for small functions or functions which used only once. For example:

some_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = filter(lambda x: x%2 == 0, some_array)

vs.

def is_even_number(x):
   return x % 2 == 0

some_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = filter(is_even_number, some_array)
1

In its simplest explanation it is a function that returns itself

So if

def y(x): return x + x

in lambda terms would be

y = lambda x: x + x

Both would return the same result if you called them

print y(5)

Would both return 10

Not the answer you're looking for? Browse other questions tagged or ask your own question.