Problem
I’m attempting to understand Python lambdas. Is lambda one of those “interesting” language terms that should be forgotten in real life?
I’m sure there are some edge instances where it’s needed, but should it be avoided due to its obscurity, the possibility of it being redefined in future releases (my assumption based on the numerous definitions), and the lower coding clarity?
Overflowing (buffer overflow) of C types – referring to the top variable and overloading to set the other field values – comes to mind. It feels like a little of tech flair, but it’s a headache for maintenance coders.
Asked by meade
Solution #1
Is it lambda expressions that you’re referring to? Like
lambda x: x**2 + 2*x - 5
Those are truly pretty beneficial. Python offers functional programming, which allows you to pass functions to other functions to do tasks. Example:
mult3 = filter(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9])
sets mult3 to [3, 6, 9], those elements of the original list that are multiples of 3. This is shorter (and, some could argue, clearer) than the last one.
def filterfunc(x):
return x % 3 == 0
mult3 = filter(filterfunc, [1, 2, 3, 4, 5, 6, 7, 8, 9])
Of course, you could do the same thing as a list comprehension in this case:
mult3 = [x for x in [1, 2, 3, 4, 5, 6, 7, 8, 9] if x % 3 == 0]
(or even range(3,10,3)), but there are many other, more advanced use cases where a list comprehension isn’t possible, and a lambda function may be the quickest method to express anything.
Lambda functions are frequently used in my code. It took me some time to get used to them, but I eventually realized that they’re an important element of the language.
Answered by David Z
Solution #2
Lambda is merely another word for function. Its name is the only thing that is mysterious, scary, or enigmatic about it. Replace lambda with function in your head as you read the following line:
>>> f = lambda x: x + 1
>>> f(3)
4
It just defines an x function. Other languages, such as R, state it explicitly:
> f = function(x) { x + 1 }
> f(3)
4
Do you see what I mean? In programming, it’s one of the most natural things to accomplish.
Answered by user251650
Solution #3
The two-line summary:
Answered by John Fouhy
Solution #4
A lambda is a component of a crucial abstraction technique for dealing with higher-order functions. Please watch high-quality classes from Abelson and Sussman and study the book SICP to obtain a clear understanding of its worth.
These are important challenges in today’s software industry, and they’re growing in popularity.
Answered by egaga
Solution #5
Lambda is unlikely to go away. See Guido’s post about how he gave up attempting to get rid of it. See a diagram of the conflict as well.
For a more detailed history of the deal underlying Python’s functional features, see this post: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html
Lambda is rarely worth it in terms of clarity, in my opinion. In most cases, there is a clearer solution that does not use lambda.
Answered by rhettg
Post is based on https://stackoverflow.com/questions/890128/how-are-lambdas-useful