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.
lambdas are extremely useful in GUI programming. For example, lets say you're creating a group of buttons and you want to use a single paramaterized callback rather than a unique callback per button. Lambda lets you accomplish that with ease:
(Note: although this question is specifically asking about
lambda
, you can also use functools.partial to get the same type of result)The alternative is to create a separate callback for each button which can lead to duplicated code.
First congrats that managed to figure out lambda. In my opinion this is really powerful construct to act with. The trend these days towards functional programming languages is surely an indicator that it neither should be avoided nor it will be redefined in the near future.
You just have to think a little bit different. I'm sure soon you will love it. But be careful if you deal only with python. Because the lambda is not a real closure, it is "broken" somehow: pythons lambda is broken
One of the nice things about
lambda
that's in my opinion understated is that it's way of deferring an evaluation for simple forms till the value is needed. Let me explain.Many library routines are implemented so that they allow certain parameters to be callables (of whom lambda is one). The idea is that the actual value will be computed only at the time when it's going to be used (rather that when it's called). An (contrived) example might help to illustrate the point. Suppose you have a routine which which was going to do log a given timestamp. You want the routine to use the current time minus 30 minutes. You'd call it like so
Now suppose the actual function is going to be called only when a certain event occurs and you want the timestamp to be computed only at that time. You can do this like so
Assuming the
log_timestamp
can handle callables like this, it will evaluate this when it needs it and you'll get the timestamp at that time.There are of course alternate ways to do this (using the
operator
module for example) but I hope I've conveyed the point.Update: Here is a slightly more concrete real world example.
Update 2: I think this is an example of what is called a thunk.
I use it quite often, mainly as a null object or to partially bind parameters to a function.
Here are examples:
to implement null object pattern:
for parameter binding:
let say that I have the following API
Then, when I wan't to quickly dump the recieved data to a file I do that:
I use lambdas to avoid code duplication. It would make the function easily comprehensible Eg:
I replace that with a temp lambda
Pretty much anything you can do with
lambda
you can do better with either named functions or list and generator expressions.Consequently, for the most part you should just one of those in basically any situation (except maybe for scratch code written in the interactive interpreter).