Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Python lambdas useful? [closed]

I'm trying to figure out Python lambdas. Is lambda one of those "interesting" language items that in real life should be forgotten?

I'm sure there are some edge cases where it might be needed, but given the obscurity of it, the potential of it being redefined in future releases (my assumption based on the various definitions of it) and the reduced coding clarity - should it be avoided?

This reminds me of overflowing (buffer overflow) of C types - pointing to the top variable and overloading to set the other field values. It feels like sort of a techie showmanship but maintenance coder nightmare.

like image 243
meade Avatar asked May 20 '09 20:05

meade


People also ask

Are Python lambdas closures?

The Python lambda function on line 4 is a closure that captures n , a free variable bound at runtime.

Why are lambdas useful in Python?

One of the foremost common use cases for lambdas is in functional programming as Python supports a paradigm (or style) of programming referred to as functional programming. It allows you to supply a function as a parameter to a different function (for example, in map, filter, etc.).

Why are lambda functions bad?

Lambdas can only return an expression Lambdas can't return complex statements, only expressions. Expressions can admittedly be rather complex, and if you try very hard you can probably accomplish the same with a lambda, but the added complexity is more of a detriment to writing clear code.

Are lambdas closures?

Lambda functions may be implemented as closures, but they are not closures themselves. This really depends on the context in which you use your application and the environment. When you are creating a lambda function that uses non-local variables, it must be implemented as a closure.


1 Answers

Are you talking about lambda expressions? Like

lambda x: x**2 + 2*x - 5 

Those things are actually quite useful. Python supports a style of programming called functional programming where you can pass functions to other functions to do stuff. 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, one could argue, clearer) than

def filterfunc(x):     return x % 3 == 0 mult3 = filter(filterfunc, [1, 2, 3, 4, 5, 6, 7, 8, 9]) 

Of course, in this particular case, you could do the same thing as a list comprehension:

mult3 = [x for x in [1, 2, 3, 4, 5, 6, 7, 8, 9] if x % 3 == 0] 

(or even as range(3,10,3)), but there are many other, more sophisticated use cases where you can't use a list comprehension and a lambda function may be the shortest way to write something out.

  • Returning a function from another function

      >>> def transform(n):   ...     return lambda x: x + n   ...   >>> f = transform(3)   >>> f(4)   7 

    This is often used to create function wrappers, such as Python's decorators.

  • Combining elements of an iterable sequence with reduce()

      >>> reduce(lambda a, b: '{}, {}'.format(a, b), [1, 2, 3, 4, 5, 6, 7, 8, 9])   '1, 2, 3, 4, 5, 6, 7, 8, 9' 
  • Sorting by an alternate key

      >>> sorted([1, 2, 3, 4, 5, 6, 7, 8, 9], key=lambda x: abs(5-x))   [5, 4, 6, 3, 7, 2, 8, 1, 9] 

I use lambda functions on a regular basis. It took me a while to get used to them, but eventually I came to understand that they're a very valuable part of the language.

like image 71
David Z Avatar answered Oct 17 '22 11:10

David Z