Why Are Python Lambdas Useful
Why Are Python Lambdas Useful
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.
(153) Well, I don't think you'd like JavaScript much then ... and good Lord, stay away from Haskell! - JAL
(238) +1 Good question - bad assumptions (obscurity of lambda) =) Try not to be judgmental of programming techniques.
Evaluate them, and add them to your mental toolkit. If you don't like them, don't use them, and be prepared to discuss them
logically without becoming religious. - Kieveli
(40) Haskell Rules! Lambda functions gives you expressivity and abstraction power. - Jonathan Barbero
(11) @JAL Not to mention LISP... - ApproachingDarknessFish
(6) @ApproachingDarknessFish "Ah, that's your father's Parenthesis. A more civilized weapon from a more civilized age." - Obi
Lisp Kenobi - Fields
when lambda is necessary ? IT IS NOT NECESSARY AT ALL - KouchakYazdi
Lambdas are very useful in code golf :) - Stephen S
Why use lambda functions? - phuclv
(1) Good question, good answers; this should not be closed. - Xiler
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:
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:
(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.
1 of 13 11/3/2018, 11:43 PM
Why are Python lambdas useful? http://www.stackprinter.com/export?question=890128&service=stacko...
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.
[1] https://docs.python.org/3.5/tutorial/controlflow.html#lambda-expressions
(13) Love the examples, very easy to understand. But for the reduce part. If I have to implement this feature. I would do
','.join(str(x) for x in [1,2,3,4,5,6,7,8,9]) - etlds
(3) BTW if you run this on Python3 you need to call list on filter results to see the actual values, also you need to import reduce
from functools - Gerard
Are we sure about the definition of "functional programming" above? It came a little bit confusing to me. - zgulser
(3) I think the key point is that lambda functions can be anonymous (as they are in all your examples). If you're assigning a
lambda function to anything then you're doing it wrong and should use def instead - Chris_Rands
(1) @zgulser It's not a definition, it's just a statement about something that functional programming allows you to do. - David Z
1
lambda is just a fancy way of saying function. Other than its name, there is nothing obscure, intimidating or
cryptic about it. When you read the following line, replace lambda by function in your mind:
>>> f = lambda x: x + 1
>>> f(3)
4
> f = function(x) { x + 1 }
> f(3)
4
(28) This is a great description for those coming from non-programming backgrounds (ie: exact sciences) which makes the
meaning of lambda very simple to understand. Thanks! - Gabriel
(8) Raymond Hettinger lamented the name in one of his talks and said that all confusion could have been avoided by naming it
'make function' instead of 'lambda'. :-) - dotslash
(7) replace lambda by function in your mind and add return before last expression - Ciprian Tomoiagă
lambdas /aren't/ functions in python, they can't contain statements. They are callable expressions (if we're being pedantically
accurate). - Aaron McMillin
(2) @AaronMcMillin Try type(lambda x: 3). lambda expressions and def statements both generate function objects; it
is only the syntax of a lambda expression that limits which instances it can produce. - chepner
@chepner Try type(lambda x: x[1] = 2) The contents of a lambda must be exactly ONE expression. This is less
expressive than the full language. - Aaron McMillin
(1) @AaronMcMillin You are missing my point. Just because you can't define every function with a lambda expression doesn't
mean the result of a lambda expression isn't a function. - chepner
2
1. Closures [1]: Very useful. Learn them, use them, love them.
2. Python's lambda keyword: unnecessary, occasionally useful. If you find yourself doing anything remotely
complex with it, put it away and define a real function.
[1] http://en.wikipedia.org/wiki/Closure_(computer_science)
2 of 13 11/3/2018, 11:43 PM
Why are Python lambdas useful? http://www.stackprinter.com/export?question=890128&service=stacko...
A lambda is part of a very important abstraction mechanism which deals with higher order functions. To get
proper understanding of its value, please watch high quality lessons from Abelson and Sussman [1], and read the
book SICP [2]
These are relevant issues in modern software business, and becoming ever more popular.
[1] http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/?
[2] http://mitpress.mit.edu/sicp/
(50) +1 for SICP. Everyone should read that book. If it doesn't kill you it WILL make you stronger. - Trey
(1) Lambda expressions are becoming popular in other languages (like C#) as well. They're not going anywhere. Reading up on
closures would be a useful exercise to understand Lambdas. Closures make a lot of coding magic possible in frameworks like
jQuery. - Dan Esparza
(13) This post does not contain an answer. It would fit better as a comment. - joar
(38) If you need to read a book everytime you ask a question on SO, it's going to get ugly. - e-satis
(30) read SICP to understand python lambda? uff.. - Daniel Magnusson
(3) Don't confuse lambdas and first-class functions. Python has an extremely limited lambda statement, but fully first-class
functions. The only difference it makes is that you have to name the functions you want to pass around. - Gareth Latty
4
I doubt lambda will go away. See Guido's post [1] about finally giving up trying to remove it. Also see an outline of
the conflict [2].
You might check out this post for more of a history about the deal behind Python's functional features:
http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html
Curiously, the map, filter, and reduce functions that originally motivated the introduction of lambda
and other functional features have to a large extent been superseded by list comprehensions and
generator expressions. In fact, the reduce function was removed from list of builtin functions in
Python 3.0. (However, it's not necessary to send in complaints about the removal of lambda, map or
filter: they are staying. :-)
My own two cents: Rarely is lambda worth it as far as clarity goes. Generally there is a more clear solution that
doesn't include lambda.
[1] http://mail.python.org/pipermail/python-dev/2006-February/060415.html
[2] https://wiki.python.org/moin/AlternateLambdaSyntax
(2) note that reduce is still importable in Python 3.0. If You REALLY want it, You can still have it. - Paweł Polewicz
(5) +1 for the link to Guido's admittance of defeat. It's nice to see so many posters agreeing with lambda! - new123456
I think Guido's attempt was more about the syntax. This person also thinks that: cackhanded.com/blog/post/2008/01/24/… -
leewz
5
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 [1] to get the
same type of result)
The alternative is to create a separate callback for each button which can lead to duplicated code.
[1] https://docs.python.org/3.5/library/functools.html#functools.partial
(1) This is exactly why I looked up what lambda was, but why does this work, to me it looks the exact same as just calling the
function straight ( stackoverflow.com/questions/3704568/… ). Maybe it's late, it does work, but why does it work? - Rqomey
(5) @Rqomey: the difference is that in this example value is defined in a loop; in the other example the parameter always only
had one value. When you add something like arg=value, you are attaching the current value to the callback. Without it, you
bind a reference to the variable in the callback. The reference will always contain the final value of the variable, since the callback
3 of 13 11/3/2018, 11:43 PM
Why are Python lambdas useful? http://www.stackprinter.com/export?question=890128&service=stacko...
happens some time after the loop has already finished running. - Bryan Oakley
(1) I just got this working yesterday, I honestly can't believe how useful it is... I can build up a menu from one for loop and a csv
file for configuration. Really useful stuff. - Rqomey
(1) Note the existence of functools.partial() which allows you to do this with less cruft (and without lambda). - Gareth
Latty
@Lattyware: why is it "less cruft"? Using functools.partial requires an extra library import, and for most simple cases
lambda is more than sufficient. - Bryan Oakley
(2) partial(my_callback, value) vs lambda arg=value: my_callback(arg) - the lambda has much more cruft
(assignment to arg and then usage) and it's less clear what the intention is (you could be doing something subtly different in the
lambda). Imports are not really a problem (you have a pile anyway and it's once per file). Code is best judged on how well it
reads, and partial() is much easier to read than the lambda. - Gareth Latty
6
a = lambda x: x + 1
print a(1)
and..
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:
>>> a = lambda x: x + 1
>>> type(a)
<type '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!" [1]:
To see how lambda is broken, try generating a list of functions fs=[f0,...,f9] where
fi(n)=i+n. First attempt:
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:
>>> n = 4
>>> [i + n for i in range(10)]
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
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 ...)
Please note that we can use lambda and still achieve the desired results in this way :
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:
w = PyQt4.QtGui.QLineEdit()
w.textChanged.connect(lambda event: dothing())
4 of 13 11/3/2018, 11:43 PM
Why are Python lambdas useful? http://www.stackprinter.com/export?question=890128&service=stacko...
Just doing w.textChanged.connect(dothing) would call the dothing method with an extra event
argument and cause an error. Using the lambda means we can tidily drop the argument without having to define
a wrapping function.
[1] http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/
your "lambda is broken" argument is broken, because of the python variable scoping rules work that way, period. You will be
bitten the same way if you created a closure inside for-loop. - Antti Haapala
(1) lambda is just python's way to provide the user with an "anonymous" function, like many other languages have (e.g.,
javascript). - Stefan Gruenwald
The lambda and function a's that you defined are not exactly same. :) They differ by __name__ field at least... - progo
It's works more than just an inline function. - kta
Your argument about "might make sense in another language" is weird. Either there are intrinsic qualities of lambdas, or they
aren't. - Titou
7
I find lambda useful for a list of functions that do the same, but for different circumstances. Like the Mozilla
plural rules [1].
plural_rules = [
lambda n: 'all',
lambda n: 'singular' if n == 1 else 'plural',
lambda n: 'singular' if 0 <= n <= 1 else 'plural',
...
]
# Call plural rule #1 with argument 4 to find out which sentence form to use.
plural_rule[1](4) # returns 'plural'
If you'd have to define a function for all of those you'd go mad by the end of it. Also it wouldn't be nice with
function names like plural_rule_1, plural_rule_2, etc. And you'd need to eval() it when you're
depending on a variable function id.
[1] https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Localization_and_Plurals
This looks similar to the brief encounters that I've had so far in F# with pattern matching and options. Do you have more info
about how to use this syntax? - Kenneth Posey
8
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).
(3) "for the most part you should just one of those in basically any situation" Period. Typing lambda's in the interpreter isn't even
that helpful. - S.Lott
(1) lambda is one of those rare features that is so hard to overuse. anywhere you can use it, it will improve readability and
maintainability. the only requisite is to really understand closures. if you don't understand them, you'll be left to believe such
nonsense as this answer. - Javier
(11) @Javier I agree with you if you are talking about "lambda" the concept; however, if we're talking about "lambda" the python
keyword then: 1) named functions are faster and can do more (statements+expressions) almost anywhere you would use
lambdas (map+filter) you can just generator expressions or list comprehensions which are more performant and concise. I'm not
saying that first class functions aren't cool, just that the "lambda" keyword in python isn't as good as just using a named
function, that's all. - Aaron Maenpaa
(3) lambda has been indispensable to me for use with functions that take callback arguments like the key= argument to sort()
and sorted() - Rick Copeland
(17) @Rick I don't doubt that, but the reality is if when you see "lambda" and you think "zohmygod lambda" and start writing
scheme code in python you will surly be disappointing by the limitations of python's lambda expression. On the other hand, if
you start out thinking to yourself "Will a list comprehension work? No. Will what I need benifit from being a named function?
No. Okay fine: sorted(xs, key = lambda x: x.name, x.height)", you will probably end up using lambda the right number of times. -
Aaron Maenpaa
@Aaron - Agreed, lambda to me is a tool of nearly last resort. But it's really handy for those last resorts. I would never use
lambda with map() or filter(), in particular. (The only reason to use map or filter, IMO, rather than a comprehension is that it
can be faster if you're using a function implemented in C, and that advantage goes away if you use lambda.) - Rick Copeland
One of my few real uses of map is in this idiom... " ".join(map(str,iterable)) and friends, where the thing to apply over the
iterable is a "type" function. - Gregg Lind
(4) +1: I cannot stress that enough when one uses a lambda one uses an nameless function. And names do have a precious
5 of 13 11/3/2018, 11:43 PM
Why are Python lambdas useful? http://www.stackprinter.com/export?question=890128&service=stacko...
I've been using Python for a few years and I've never run in to a case where I've needed lambda. Really, as the
tutorial [1] states, it's just for syntactic sugar.
[1] http://docs.python.org/tutorial/controlflow.html#lambda-forms
(8) They are very useful when developing a GUI using python. Often, widgets need a reference to a function. If you need a widget
to call a function and pass arguments, lambda is a very convenient way to do that. - Bryan Oakley
What's the advantage over passing multiple arguments into the function? func_name(a,b): return a+b rather than using lambda
- dter
it's not needed but it helps to write shorter code and more readable in many cases, esp. in verbose languages like Java or C++ -
phuclv
10
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 [1] and the map function [2] (that links to python docs, but it exists in nearly
every language that supports functional constructs) to see why it's useful.
[1] http://en.wikipedia.org/wiki/Closure_(computer_science)
[2] http://docs.python.org/tutorial/datastructures.html
(1) That stuff can be done without lambdas. It's just a big hassle. - Brian
11
That's it. For example, let's supose you have your main function and need to square values. Let's see the
traditional way and the lambda way to do this:
Traditional way:
def main():
...
...
y = square(some_number)
...
return something
def square(x):
return x**2
def main():
...
square = lambda x: x**2
y = square(some_number)
return something
Lambda functions go very well with lists, like lists comprehensions or map. In fact, list comprehension it's a
"pythonic" way to express yourself using lambda. Ex:
>>>a = [1,2,3,4]
>>>[x**2 for x in a]
[1,4,9,16]
[] : "Give me a list"
6 of 13 11/3/2018, 11:43 PM
Why are Python lambdas useful? http://www.stackprinter.com/export?question=890128&service=stacko...
That's convenient uh? Creating functions like this. Let's rewrite it using lambda:
Now let's use map, which is the same thing, but more language-neutral. Maps takes 2 arguments:
(ii) an iterable
And gives you a list where each element it's the function applied to each element of the iterable.
>>> a = [1,2,3,4]
>>> squared_list = map(lambda x: x**2, a)
If you master lambdas and mapping, you will have a great power to manipulate data and in a concise way.
Lambda functions are neither obscure nor take away code clarity. Don't confuse something hard with something
new. Once you start using them, you will find it very clear.
12
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.
[1] http://www.mail-archive.com/[email protected]/msg02084.html
[2] https://en.wikipedia.org/wiki/Thunk
13
As stated above, the lambda operator in Python defines an anonymous function, and in Python functions are
closures. It is important not to confuse the concept of closures with the operator lambda, which is merely
syntactic methadone for them.
When I started in Python a few years ago, I used lambdas a lot, thinking they were cool, along with list
comprehensions. However, I wrote and have to maintain a big website written in Python, with on the order of
several thousand function points. I've learnt from experience that lambdas might be OK to prototype things with,
7 of 13 11/3/2018, 11:43 PM
Why are Python lambdas useful? http://www.stackprinter.com/export?question=890128&service=stacko...
but offer nothing over inline functions (named closures) except for saving a few key-stokes, or sometimes not.
it is easier to read software that is explicitly written using meaningful names. Anonymous closures by
definition cannot have a meaningful name, as they have no name. This brevity seems, for some reason, to
also infect lambda parameters, hence we often see examples like lambda x: x+1
it is easier to reuse named closures, as they can be referred to by name more than once, when there is a
name to refer to them by.
it is easier to debug code that is using named closures instead of lambdas, because the name will appear in
tracebacks, and around the error.
That's enough reason to round them up and convert them to named closures. However, I hold two other grudges
against anonymous closures.
The first grudge is simply that they are just another unnecessary keyword cluttering up the language.
The second grudge is deeper and on the paradigm level, i.e. I do not like that they promote a functional-
programming style, because that style is less flexible than the message passing, object oriented or procedural
styles, because the lambda calculus is not Turing-complete (luckily in Python, we can still break out of that
restriction even inside a lambda). The reasons I feel lambdas promote this style are:
There is an implicit return, i.e. they seem like they 'should' be functions.
They are an alternative state-hiding mechanism to another, more explicit, more readable, more reusable
and more general mechanism: methods.
I try hard to write lambda-free Python, and remove lambdas on sight. I think Python would be a slightly better
language without lambdas, but that's just my opinion.
(1) "...in Python functions are closures". That's not quite right, as I understand it. Closures are functions, but functions are not
always closures. Function-> lambda x,y: x+y. Closure-> lambda x: lambda y: x+y - 0atman
(6) "because the lambda calculus is not Turing-complete" is plain wrong, untyped lambda calculus IS Turing complete, that is
the reason why it is so significant. You can get recursion using the Y-combinator, Y = lambda f: (lambda x:
x(x))(lambda y: f(lambda *args: y(y)(*args))) - Antti Haapala
Furthermore, if one goes to Wikipedia to read on Turing completeness, it says "A classic example is the lambda calculus." - Antti
Haapala
(6) Good Lord, answer that claims lambda-calculus is not Turing-complete with 10 upvotes?! - Marcin Łoś
seriously - Not Turing complete - this answer needs a serious edit or retraction. - Tony Suffolk 66
@MarcinŁoś Up-voted because it adheres to KISS. As a comparison, the K&R book mentions that while using assignment as part
or a larger expression is more compact, it is often less readable. The trend-wagon of using functional programming patterns is
trite and cliche. It is sufficient to state how they can be used, but it is overzealous to state that they are paramount to becoming a
competent developer; completely subjective. This argument is analogous to C++ developers who argue that languages w/o
classes are primitive, inferior and inadequate. "Turing-completeness", arguments are pedantic. - typedeaf
14
Lambdas are actually very powerful constructs that stem from ideas in functional programming, and it is
something that by no means will be easily revised, redefined or removed in the near future of Python. They help
you write code that is more powerful as it allows you to pass functions as parameters, thus the idea of functions as
first-class citizens.
Lambdas do tend to get confusing, but once a solid understanding is obtained, you can write clean elegant code
like this:
The above line of code returns a list of the squares of the numbers in the list. Ofcourse, you could also do it like:
def square(x):
return x*x
It is obvious the former code is shorter, and this is especially true if you intend to use the map function (or any
similar function that takes a function as a parameter) in only one place. This also makes the code more intuitive
and elegant.
Also, as @David Zaslavsky mentioned in his answer, list comprehensions are not always the way to go especially
8 of 13 11/3/2018, 11:43 PM
Why are Python lambdas useful? http://www.stackprinter.com/export?question=890128&service=stacko...
if your list has to get values from some obscure mathematical way.
From a more practical standpoint, one of the biggest advantages of lambdas for me recently has been in GUI and
event-driven programming. If you take a look at callbacks in Tkinter, all they take as arguments are the event that
triggered them. E.g.
def define_bindings(widget):
widget.bind("<Button-1>", do-something-cool)
def do-something-cool(event):
#Your code to execute on the event trigger
Now what if you had some arguments to pass? Something as simple as passing 2 arguments to store the
coordinates of a mouse-click. You can easily do it like this:
def main():
# define widgets and other imp stuff
x, y = None, None
widget.bind("<Button-1>", lambda event: do-something-cool(x, y))
Now you can argue that this can be done using global variables, but do you really want to bang your head
worrying about memory management and leakage especially if the global variable will just be used in one
particular place? That would be just poor programming style.
In short, lambdas are awesome and should never be underestimated. Python lambdas are not the same as LISP
lambdas though (which are more powerful), but you can really do a lot of magical stuff with them.
Thanks. I completely didn't understand your last example. How come x and y are defined in both main and
do_something_cool? What happens to x and y in the function? The values passed seem to be immediately overwritten? How
does the function know about event? Could you add some comments / explanation? Thanks - Sanjay Manohar
@SanjayManohar I am passing x and y as arguments to do-something-cool and their values are being set in the function to
illustrate how you can use lambdas to pass arguments where none are expected. The widget.bind function expects a event
parameter which identifies the GUI event on that particular widget. I recommend reading on Tkinter's programming model for
greater clarity. - varagrawal
hmm I think I understand the Tkinter model. But still don't quite understand -- you pass x,y then do x=event.x. Doesn't that
overwrite the value you passed? And how does the function know what event is? I don't see where you pass that to the function.
Or is it a method? Also are you allowed minus signs in a function name? - Sanjay Manohar
@SanjayManohar buddy you need to read up on Tkinter and Python. Tkinter passes the event object the function as a default
action. As for the x, y, that is just an example for illustrative purposes. I am trying to show the power of lambdas, not Tkinter.
:) - varagrawal
(1) OK now I see what you intended - you want the function to receive event? in that case, shouldn't your lambda read lambda
event: do_something_cool(event,x,y) ? - Sanjay Manohar
@SanjayManohar you clearly haven't read about the Tkinter model. I suggest you RTFM on tkinter before asking a redundant
question like that. - varagrawal
My understanding is that tkinter will call your function with event as a parameter. Your function do-something-cool takes 3
parameters, but you only pass it two, x and y. Have I missed something? Have you tested this code? - Sanjay Manohar
So the lambda takes the event parameter and passes that to do-something-cool. Hence do-something-cool takes 3
parameters. That is in fact how lambdas work and yes, I have used this code in an application and then posted here. :) -
varagrawal
@SanjayManohar - sorry this is much later, but in fact the lambda is only passing two variables, so do-something-cool should
take only two parameters, there is a bug in the example. Also dashes are invalid in function names so this code was never tested.
- Jeremy Sorensen
@JeremySorensen Great - thanks, I thought I was going mad! - Sanjay Manohar
15
Lambdas are deeply linked to functional programming style in general. The idea that you can solve problems by
applying a function to some data, and merging the results, is what google uses to implement most of its
algorithms.
Programs written in functional programming style, are easily parallelized and hence are becoming more and
more important with modern multi-core machines. So in short, NO you should not forget them.
16
9 of 13 11/3/2018, 11:43 PM
Why are Python lambdas useful? http://www.stackprinter.com/export?question=890128&service=stacko...
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 [1]
[1] http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/
Python's lambda is not broken. There are two ways to handle locals in lambdas. Both have advantages and disadvantages. I think
the approach Python (and C#) took is probably counterintuitive to those more accustomed to purely functional languages, since
with a purely functional language I don't think that approach even makes sense. - Brian
It is indeed counterintuitive. I'm not a python programmer but in squeak smalltalk it is the same and I stumple upon this
regularly. So even I would consider it "broken" :) - Norbert Hartl
No, this has nothing to do with counterintuitivity. It is just that the lexical scope of variable names is that of a function; a loop
does not introduce a lexical scope. Functions also work the same way in Javascript. If you need a scope for var, you can always do
(lambda scopedvar: lambda x: scopedvar + x)() - Antti Haapala
17
I'm just beginning Python and ran head first into Lambda- which took me a while to figure out.
Note that this isn't a condemnation of anything. Everybody has a different set of things that don't come easily.
Is lambda one of those 'interesting' language items that in real life should be forgotten?
No.
I'm sure there are some edge cases where it might be needed, but given the obscurity of it,
It's not obscure. The past 2 teams I've worked on, everybody used this feature all the time.
the potential of it being redefined in future releases (my assumption based on the various
definitions of it)
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.
This reminds me of overflowing (buffer overflow) of C types - pointing to the top variable and
overloading to set the other field values...sort of a techie showmanship but maintenance coder
nightmare..
Lambda is like buffer overflow? Wow. I can't imagine how you're using lambda if you think it's a "maintenance
nightmare".
(5) -1 for making me (and others) read the whole question again. Note that others managed to answer without doing it. - Paweł
Polewicz
18
I started reading David Mertz's book today 'Text Processing in Python.' While he has a fairly terse description of
Lambda's the examples in the first chapter combined with the explanation in Appendix A made them jump off the
page for me (finally) and all of a sudden I understood their value. That is not to say his explanation will work for
you and I am still at the discovery stage so I will not attempt to add to these responses other than the following: I
am new to Python I am new to OOP Lambdas were a struggle for me Now that I read Mertz, I think I get them
and I see them as very useful as I think they allow a cleaner approach to programming.
He reproduces the Zen of Python, one line of which is Simple is better than complex. As a non-OOP programmer
reading code with lambdas (and until last week list comprehensions) I have thought-This is simple?. I finally
realized today that actually these features make the code much more readable, and understandable than the
alternative-which is invariably a loop of some sort. I also realized that like financial statements-Python was not
designed for the novice user, rather it is designed for the user that wants to get educated. I can't believe how
powerful this language is. When it dawned on me (finally) the purpose and value of lambdas I wanted to rip up
10 of 13 11/3/2018, 11:43 PM
Why are Python lambdas useful? http://www.stackprinter.com/export?question=890128&service=stacko...
19
I can give you an example where I actually needed lambda serious. I'm making a graphical program, where the
use right clicks on a file and assigns it one of three options. It turns out that in Tkinter (the GUI interfacing
program I'm writing this in), when someone presses a button, it can't be assigned to a command that takes in
arguments. So if I chose one of the options and wanted the result of my choice to be:
Then no big deal. But what if I need my choice to have a particular detail. For example, if I choose choice A, it
calls a function that takes in some argument that is dependent on the choice A, B or C, TKinter could not support
this. Lamda was the only option to get around this actually...
(1) Well, presumably you could have done def foo... and then passed in foo instead of lambda. It's just more code and you
have to come up with a name. - Jon Coombs
20
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 imagine loop_dic being very long. If you would just use a plain value that
includes i instead of the lambda version of that value you would get a NameError.
>>> new_lis
[{'name': 'Peter Wallace'}, {'name': 'Josef Wallace'}]
Instead of
>>> new_lis
[{'name': 'Peter Wallace'}, {'name': 'Josef Wallace'}]
21
I use lambdas to avoid code duplication. It would make the function easily comprehensible Eg:
def a_func()
...
if some_conditon:
...
call_some_big_func(arg1, arg2, arg3, arg4...)
else
...
call_some_big_func(arg1, arg2, arg3, arg4...)
def a_func()
...
call_big_f = lambda args_that_change: call_some_big_func(arg1, arg2, arg3, args_that_change)
if some_conditon:
...
call_big_f(argX)
else
...
call_big_f(argY)
11 of 13 11/3/2018, 11:43 PM
Why are Python lambdas useful? http://www.stackprinter.com/export?question=890128&service=stacko...
22
I'm a python beginner, so to getter a clear idea of lambda I compared it with a 'for' loop; in terms of efficiency.
Here's the code (python 2.7) -
import time
start = time.time() # Measure the time taken for execution
def first():
squares = map(lambda x: x**2, range(10))
# ^ Lambda
end = time.time()
elapsed = end - start
print elapsed + ' seconds'
return elapsed # gives 0.0 seconds
def second():
lst = []
for i in range(10):
lst.append(i**2)
# ^ a 'for' loop
end = time.time()
elapsed = end - start
print elapsed + ' seconds'
return elapsed # gives 0.0019998550415 seconds.
(6) You may be interested in the timeit module, which typically gives more accurate results than subtracting time.time()
values. - Kevin
(1) Hmm, don't you need to restart your timer at the start of first() and second()? - qneill
23
I use it quite often, mainly as a null object [1] or to partially bind parameters to a function.
class X(object):
#...
def packet_received(data):
# some kind of preprocessing
self.callback(data)
#...
Then, when I wan't to quickly dump the recieved data to a file I do that:
dump_file = file('hex_dump.txt','w')
X.callback = lambda (x): dump_hex(dump_file, x)
...
dump_file.close()
[1] http://en.wikipedia.org/wiki/Null_Object_pattern
24
12 of 13 11/3/2018, 11:43 PM
Why are Python lambdas useful? http://www.stackprinter.com/export?question=890128&service=stacko...
I use lambda to create callbacks that include parameters. It's cleaner writing a lambda in one line than to write a
method to perform the same functionality.
For example:
import imported.module
def func():
return lambda: imported.module.method("foo", "bar")
as opposed to:
import imported.module
def func():
def cb():
return imported.module.method("foo", "bar")
return cb
25
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.
26
13 of 13 11/3/2018, 11:43 PM