LAMBDA IS IT USELESS?

Alex Martelli aleaxit at yahoo.com
Fri Jul 13 12:01:37 EDT 2001


"Suchandra Thapa" <ssthapa at classes.cs.uchicago.edu> wrote in message
news:slrn9ktu24.8c3.ssthapa at hepcat.telocity.com...
    ...
>     The lambda can be  a way  to avoid the need to define and name trivial
> functions that get used in only one place.

And this, in Python, is the ONLY thing that is specific to lambda.

> However, the lambda function
> is a also a way to dynamically create functions as well as a way of
> composing two functions together.

...which you can perfectly well do with other callables besides
lambda forms -- as long as you don't mind naming them.

> For example, suppose you need to
> apply a scaling factor to a list but the scaling factor depends on
> user input.  In this case, with lambda you can do a with
> lambda x: x * s where the value of s depends on the user input.  Also,

And without lambda you can do it with a def:
    def mpy(x): return x*s
    # and now use mpy wherever you'd use your lambda
If x is visible inside the lambda (nested-scopes are active) it will
be identically visible inside mpy.  If you need to 'freeze' s's value,
you will supply an s=s defaulting-parameter identically to the lambda
or to the def.  The ONLY difference is that lambda generates an
anonymous function (and is an expression), while def generates a
named function (and is a statement).

> suppose you have two image filters and you want to create a new filter
> that applies the first filter then the second one.  Then using lambda,
> you can just use lambda x: filter2(filter1(x)).

And without lambda you can do it with a def:
    def combined_filter(x): return filter2(filter1(x))
with all the other considerations being the same as above.

>     Incidentally, you can use lambda to do a bunch of different things
including
> reducing the number of parameters a function takes (currying).  E.g.

The modest amount of currying you can do with a lambda, you can
just as well do with a def.  def gives more powerful currying
because you can have *args and **kwds in a def, which lambda
does not support.  See for example the entry on currying in the
Python Cookbook:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52549
and notice that not one of the entries uses lambda: there's
a class and a couple of variant of closures (obtained with
def, NOT with lambda: I don't think you could reach this level
of generality with a lambda in Python, given its limitations!).

> suppose foo takes x, y, and z then lambda x, y:foo(x, y, 4) returns a
function
> that takes only two paramters.
>     Basically, think of lambda as a way of applying mathematical
operations
> like +, /, * as well as a few others to functions.

I suggest one should instead think of lambda ONLY as what it
is: a way to leave particularly-simple functions (those that
can fit in a single expression) unnamed (in a way that is
syntactically an expression).  There is no other added value
of lambda wrt def in Python, and there are several aspects
where lambda _subtracts_ value...


Alex






More information about the Python-list mailing list