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.
Lambda is a procedure constructor. You can synthesize programs at run-time, although Python's lambda is not very powerful. Note that few people understand that kind of programming.
In Python,
lambda
is just a way of defining functions inline,and..
..are the exact same.
There is nothing you can do with lambda which you cannot do with a regular function—in Python functions are an object just like anything else, and lambdas simply define a function:
I honestly think the
lambda
keyword is redundant in Python—I have never had the need to use them (or seen one used where a regular function, a list-comprehension or one of the many builtin functions could have been better used instead)For a completely random example, from the article "Python’s lambda is broken!":
I would argue, even if that did work, it's horribly and "unpythonic", the same functionality could be written in countless other ways, for example:
Yes, it's not the same, but I have never seen a cause where generating a group of lambda functions in a list has been required. It might make sense in other languages, but Python is not Haskell (or Lisp, or ...)
Edit:
There are a few cases where lambda is useful, for example it's often convenient when connecting up signals in PyQt applications, like this:
Just doing
w.textChanged.connect(dothing)
would call thedothing
method with an extraevent
argument and cause an error. Using the lambda means we can tidily drop the argument without having to define a wrapping function.Note that this isn't a condemnation of anything. Everybody has a different set of things that don't come easily.
No.
It's not obscure. The past 2 teams I've worked on, everybody used this feature all the time.
I've seen no serious proposals to redefine it in Python, beyond fixing the closure semantics a few years ago.
It's not less clear, if you're using it right. On the contrary, having more language constructs available increases clarity.
Lambda is like buffer overflow? Wow. I can't imagine how you're using lambda if you think it's a "maintenance nightmare".
A useful case for using lambdas is to improve the readability of long list comprehensions. In this example
loop_dic
is short for clarity but imagineloop_dic
being very long. If you would just use a plain value that includesi
instead of the lambda version of that value you would get aNameError
.Instead of
I can't speak to python's particular implementation of lambda, but in general lambda functions are really handy. They're a core technique (maybe even THE technique) of functional programming, and they're also very useuful in object-oriented programs. For certain types of problems, they're the best solution, so certainly shouldn't be forgotten!
I suggest you read up on closures and the map function (that links to python docs, but it exists in nearly every language that supports functional constructs) to see why it's useful.
lambda
is just a fancy way of sayingfunction
. Other than its name, there is nothing obscure, intimidating or cryptic about it. When you read the following line, replacelambda
byfunction
in your mind:It just defines a function of
x
. Some other languages, likeR
, say it explicitly:You see? It's one of the most natural things to do in programming.