[Tutor] fancy list things
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Thu Feb 5 20:22:50 EST 2004
> > In Python lambda can be used to build simple (very simple sadly)
> > anonymous expressions (no statements) which can get used at places
> > where you think it's not worth writing a function with a name. But
> > their usage is very weakly in Python, you can't compare it with
> > languages like Lisp (CL, Scheme) or Haskell. But that's no bug it's a
> > feature :-)
>
> Is it only useful in map() and filter() and reduce()? And other places
> where you want to hand a little function to a function? comp() for
> example. Is there another class of examples?
Hi Marilyn,
lambda's can also be useful whenever we want to create simpler versions of
functions that already have some parameters filled in. Here is a simple
example:
###
>>> def add(x, y):
... return x + y
...
>>> add1 = lambda y: add(1, y)
>>>
>>>
>>> add1(42)
43
###
Another way of saying this is:
###
def add1(y):
return add(1, y)
###
The 'def' statement in Python is sort of a combination of
lambda-function-making plus name-binding.
> I'm teaching python for the first time and want to be as solid as
> possible on this stuff.
Hmmm... you may want to avoid talking about lambda, then. Concentrate on
what you're comfortable talking about. I'd recommend focusing on the
things that people will usually do with Python, so show how to construct
functions with 'def'.
The advantage that 'lambda' has over 'def' is that it doesn't force us to
bind the function value with a name. But Python programmers usually want
to give good names our functions to express human intent, anyway.
And function values are just function values. *grin* def'ed functions can
also be passed around just as easily as values from lambdas:
###
>>> def compose(f, g):
... def composed_function(x):
... return f(g(x))
... return composed_function
...
>>> def square(x):
... return x * x
...
>>> def sqrt(x):
... return x ** (0.5)
...
>>> quad_power = compose(square, square)
>>> quad_power(17)
83521
>>> identity = compose(square, sqrt)
>>> identity(17)
17.0
###
So there's no need to concentrate on the foreignness of the word 'lambda':
lambda just creates function values. But we can get the value of anything
by just naming it:
###
>>> x = 42
>>> x
42
>>> def square(x): return x * x
...
>>> square
<function square at 0x8157bfc>
###
Just for reference: the same thing that we did with 'def' here can be done
exclusively with lambda's.
###
>>> compose = lambda f, g: lambda x: f(g(x))
>>> square = lambda x: x*x
>>> sqrt = lambda x: x**(0.5)
>>> quad_power = compose(square, square)
>>> quad_power(17)
83521
>>> quarter_power = compose(sqrt, sqrt) ## with cheese?
>>> quarter_power(16)
2.0
###
The advantage of lambda here is that it's concise, so this kind of
function-fiddling code ends up being really short to write. To a eye
trained in functional languages, it looks neater. But there's little that
we're doing here that we couldn't do already with 'def'.
Hope this helps!
More information about the Tutor
mailing list