Would Anonymous Functions Help in Learning Programming/Python?

Paul Rubin http
Sat Sep 22 14:52:15 EDT 2007


Bryan Olson <fakeaddress at nowhere.org> writes:
> chris.monsanto at gmail.com wrote:
> > There are already anonymous functions in Python.
> > lambda x, y, z: x + y + z
> > is the same as:
> > def _(x, y, z): return x + y + z
> 
> They are the same only in special cases:
>      The special identifier "_" is used in the interactive
>      interpreter to store the result of the last evaluation...

I'm not sure what Chris was really getting at, since among other
things lambda:... is an expression while def... is a statement.
But Python certainly has anonymous functions without lambda:

   def compose(f,g):
     def h(*a, **k):
       return f(g(*a, **k))
     return h

   x = compose(math.sin, math.cos)(1.0)

computes sin(cos(1.0)) where a function equivalent to 
  lambda x: sin(cos(x))
is constructed and used without being bound to a symbol.



More information about the Python-list mailing list