lambda in list comprehension acting funny

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jul 12 02:52:08 CEST 2012


On Wed, 11 Jul 2012 13:21:34 -0700, John Ladasky wrote:

> Exactly.  It's threads like these which remind me why I never use
> lambda.  I would rather give a function an explicit name and adhere to
> the familiar Python syntax, despite the two extra lines of code.

lambda is familiar Python syntax, or at least it should be if you 
consider yourself an expert Python programmer.

And besides, this is not a problem with lambda. You get the *exact* same 
problem with "ordinary" function definitions. Of course you do -- lambdas 
*are* ordinary functions, they just have a more compact syntax.

funcs = []
for i in range(5):
    def pwr(x):
        return x**i
    print("Testing 3**%d:" % i, pwr(3))
    funcs.append(pwr)

for j in range(5):
    print("Expected", 3**j, "got", funcs[j](3))

gives you *exactly* the same issue as with the lambda. This is not a 
problem with lambdas, this is a scoping issue.


> I don't even like the name "lambda".  It doesn't tell you what it is

Whereas "def" does?

If you can learn that "def" defines a function (as opposed to a class, a 
module, or anything else really) than it's not that hard to learn that 
"lambda" also defines a function.

Although I do wonder whether people would be less scared of lambda, and 
less likely to imagine that it creates something with strange mysterious 
properties different from "regular functions", if the keyword was called 
"function" instead of lambda?


> (unless you're John McCarthy), a function that you won't re-use and so
> you don't really need to give it a persistent name.

Whether or not a function has a persistent name has nothing to do with 
reuse. Anonymous functions can be reused. The driving motivation for 
lambdas is for callback functions, which are frequently reused, but you 
don't need or want to fill your global namespace up with potentially 
hundreds of callback functions.

Lambda syntax is only incidentally good for saving a line or two. If the 
only benefit of lambda was to save a line of code, that would be stupid. 
The real reason for lambda is to avoid polluting your namespace with 
unnecessary names.

Python allows us to easily and simply use anonymous strings, anonymous 
ints, anonymous objects of all types. Why should functions be treated as 
second-class?


> I haven't seen any lambdas in any Python library code, or in any of the
> third-party modules I use (numpy, matplotlib, Biopython).  Do they
> exist?  Because I have not been forced to do so, I haven't retained a
> space in the top drawer of my programming brain for lambda.

There are over 800 uses of lambda in the standard library:

[steve at ando ~]$ cd /usr/local/lib/python3.2
[steve at ando python3.2]$ grep lambda *.py */*.py | wc -l
829


(although some of the above are comments, doctests, etc.)

See, for example, configparser, decimal, functools, gettext, inspect, 
pydoc, symtable, uuid, and others. pydoc and idlelib in particular make 
heavy use of lambda, and the test suite is overflowing with them -- over 
600 uses in total.


-- 
Steven



More information about the Python-list mailing list