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

Josiah Carlson jcarlson at uci.edu
Sun Jun 19 18:45:00 CEST 2005


Donovan Baarda <abo at minkirri.apana.org.au> wrote:
> Josiah Carlson wrote:
> > Donovan Baarda <abo at minkirri.apana.org.au> wrote:
> > 
> >>Nick Coghlan wrote:
> >>
> >>>Donovan Baarda wrote:
> [...]
> >>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.
> 
> in the end though, a sequence of statements that completes with a 
> "return value" is, when treated as a black box, indistinguishable from 
> an expression. Originally I thought that this also had to be qualified 
> with "and has no side-effects", but I see now that is not the case.

Lambdas/anonymous functions are generally used when they are simple and
are either to be applied _now_, or passed as a callback to someone else
(sometimes people use the x = lambda..., but that is laziness,
def/return is clearer).  Are either of the following two examples easy
to read?

rslt = (def (arg):
            #body that handles arg
            ...
       )(argument_passed_to_fcn)

foo((def (arg):
        #body that handles arg
        ...
     ), foo_arg, ...)

I personally think they are horrible.  Both of the above would be easier
to read as...

def _throw_away(arg):
    #body that handles arg
    ...

rslt = _throw_away(argument_passed_to_fcn)
foo(_throw_away, ...)


> [...]
> >>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.
> 
> Yeah, but we don't use "def" to bind arbitary variables, only 
> functions/procedures. So in python, they are intimately identified with 
> functions and procedures.

Being that this entire thread is about functions, perhaps there was an
implied "function" in there?  Also, with the right decorator, it becomes
an arbitrary assignment method in Python 2.4 ...

>>> def assign(val):
...     return lambda a:val
...
>>> @assign(5)
... def foo(arg): pass
...
>>> foo
5
>>>


> >>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 was thinking that this is up to the interpreter... depending on what 
> the practical limitations are that cause the limitation in the first 
> place. For example... if it can't be reduced to an "expression" through 
> simple transforms.

Care to write the transformation library for all Python statements? Care
to see that multi-line anonymous function I described above? I
certainly hope not.


 - Josiah



More information about the Python-Dev mailing list