[Tutor] fancy list things

Marilyn Davis marilyn at deliberate.com
Fri Feb 6 15:38:30 EST 2004


> 
> 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
> ###

Nice.  
> 
> 
> 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

Oh.  I don't mind exposing my ignorance (obviously).  It's a good
teaching/learning technique.  And it encourages students to expose
their ignorance.

> things that people will usually do with Python, so show how to construct
> functions with 'def'.

But if people don't actually use a particular feature, then I don't
want to waste time with it.  That's what I'm finding out.

The Deitel book barely mentions lambda on page 1249!:

"Python2.2's nested-scope behavior also makes it easier for
programmers to write lambda expressions.  A lambda expression-- when
evaluated-- produces a function, in much the same way (some named
function) produces a function.  Many programmers use lambda expresson
to define callbacks for GUI applications.  In this text we have chosen
to focus on object-orented programming in Python, rather than on
functional or procedural programming."

And that is all it says.  So I'm asking here.

> 
> 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!
> 

It's a cool example.  Some of my students are perl programmers.  They
might want to know that lambda exists, anyway.  And some here have
shown examples where it makes code easier to read.

Anyway, I certainly appreciate all the generous help here.

Marilyn Davis

> 

-- 






More information about the Tutor mailing list