[Tutor] Passing perimeters in dictionary values?

Alan Gauld alan.gauld at btinternet.com
Wed Feb 25 01:01:49 CET 2009


"nathan virgil" <sdragon1984 at gmail.com> wrote 

> I'm not familiar with lambdas yet, and I don't think this book will
> introduce me to them; they aren't listed in the index, anyway.

lambda is just a fancy mathematical name for a simple 
concept - its a block of code, like the body of a function.
In Python its even simpler, it is an expression. And we 
store that expression and make it callable.


def funcname(aParam):
      return <expression using aParam>

is the same as

funcname = lambda aParam: <expression using aParam>

In some languages, including some Lisp dialects function 
definitions are syntactic sugar for exactly that kind of lambda 
assignment. Some even make it explicit as in:

(define func
       ( lambda (aParam) 
             (expression using aParam)))

In Python its more like the other way around, functions are 
defined using def and lambdas are really syntax wrapped 
around that. 

lambdas are often offputting to beginners but they are useful 
in avoiding lots of small oneliner type functions (and the resultant 
namespace clutter) and they are very important in understanding
the theory behind computing (ie. Lambda calculus).

HTH,


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list