[Python-Dev] Recommend accepting PEP 312 --Simple Implicit Lambda

Josiah Carlson jcarlson at uci.edu
Sun Jun 19 10:01:47 CEST 2005


Donovan Baarda <abo at minkirri.apana.org.au> wrote:
> Nick Coghlan wrote:
> > Donovan Baarda wrote:
> > 
> >>As I see it, a lambda is an anonymous function. An anonymous function is 
> >>a function without a name.
> > 
> > 
> > And here we see why I'm such a fan of the term 'deferred expression' 
> > instead of 'anonymous function'.
> 
> But isn't a function just a deferred expression with a name :-)

A function in Python is actually a deferred sequence of statements and
expressions. An anonymous function in Python (a lambda) is a deferred
expression.


> > Python's lambda expressions *are* the former, but they are 
> > emphatically *not* the latter.
> 
> Isn't that because lambda's have the limitation of not allowing 
> statements, only expressions? I know this limitation avoids side-effects 
> and has significance in some formal (functional?) languages... but is 
> that what Python is? In the Python I use, lambda's are always used where 
> you are too lazy to define a function to do it's job.

I've generally seen people use lambdas for things that don't require
names in the current context; i.e. callbacks with simple executions.


> To me, anonymous procedures/functions would be a superset of "deferred 
> expressions", and if the one stone fits perfectly in the slingshot we 
> have and can kill multiple birds... why hunt for another stone?

Are "deferred expressions" perhaps another way of spelling "function
closure"?


> Oh yeah Raymond: on the "def defines some variable name"... are you 
> joking? You forgot the smiley :-)

'def' happens to bind the name that follows the def to the function with
the arguments and body following the name.


> I don't get what the problem is with mixing statement and expression 
> semantics... from a practial point of view, statements just offer a 
> superset of expression functionality.

Statements don't have a return value.  To be more precise, what is the
value of "for i in xrange(10): z.append(...)"?  Examine the selection of
statements available to Python, and ask that question.  The only one
that MAY have a return value, is 'return' itself, which really requires
an expression to the right (which passes the expression to the right to
the caller's frame).  When you have statements that ultimately need a
'return' for a return value; you may as well use a standard function
definition.


> If there really is a serious practical reason why they must be limited 
> to expressions, why not just raise an exception or something if the 
> "anonymous function" is too complicated...

Define "too complicated"?


> I did some fiddling and it seems lambda's can call methods and stuff 
> that can have side effects, which kinda defeats what I thought was the 
> point of "statements vs expressions"... I guess I just don't 
> understand... maybe I'm just thick :-)

There is nothing stopping anyone from modifying anything in a lambda...

a = list(...)
pop = lambda:a.pop()
lcycle = lambda: a and a.append(a.pop(0))
rcycle = lambda: a and a.insert(0, a.pop())
popany = lambda: a and a.pop(random.randrange(len(a)))


 - Josiah



More information about the Python-Dev mailing list