Draft Pep (was: Re: Let's Talk About Lambda Functions!)

John Roth johnroth at ameritech.net
Sun Aug 4 21:41:24 EDT 2002


"Steve Holden" <sholden at holdenweb.com> wrote in message
news:mAb39.240112$724.131601 at atlpnn01.usenetserver.com...
> "John Roth" <johnroth at ameritech.net> wrote ...
> > "Steve Holden" <sholden at holdenweb.com> wrote ...
>
> There's probably some detail of your proposal that I'm missing, but I
really
> do believe it's dead in the water. You could produce a PEP, that at
least
> would have the merit of making your proposal (and the reasons for its
> rejection, should it be rejected) fully explicit.

PEP: xxx
Title: Anonymous Functions and Classes
Version: $Revision: 1.0 $
Last-Modified: $Date: 2002/08/03 16:23:15 $
Author: John H. Roth <pythonstuff at jhrothjr.com>
Status: Active
Type: Standards Track
Created: 03-Aug-2002
Post-History:


Abstract

    This PEP provides the syntax for generalized anonymous functions and
    classes.  It provides a replacement for the lambda() function.


Rationale

    There is a continuous interest in generalizing the lambda()
function,
    which has significant limitations.  Most of the proposals to date
have
    attempted to invent additional syntax to allow fewer restrictions
while
    still maintaining the expression format of lambda().

    This proposal short circuits this by using the statement form of
def,
    including indentation syntax, within an expression.

    It also provides a tutorial function, in that programmers coming to
Python
    without experience in a language that includes first class functions
    frequently miss the fact that functions and classes can be rebound.
This
    makes the fact explicit.


Specification

    An anonymous function or class consists of a parenthesized function
or
    class definition, written the same way as a normal function or class
    definition, with the following exceptions.

    1. The name (the token immediately following the word def or class)
is
    obviously omitted.

    2. The 'def' or 'class' token must be on the line following the
opening
    parenthesis.

    3. The 'def' or 'class' token must be indented sufficiently for the
ending
    right parenthesis to close the definition, and still continue the
enclosing
    containing expression.

    A very simple example:
        x = (
            def (x, y):
                print x
                print y
            )

    This example creates an anonymous function and then binds it to the
    variable x.  If x was found within a class, it would be a method.


Backward Compatibility.
0        1         2         3         4         5         6         7
123456789012345678901234567890123456789012345678901234567890123456789012
3456789

    There are no backward compatibility issues.  There are no new
keywords,
    and it does not change the interpretation of any currently valid
syntax.

    However, it might break syntax aware editors.

    It is not part of this PEP to either remove or depreciate lambda().


Issues

    There is a significant issue as to whether this is, in fact,
necessary.
    A number of the contributors to the discussion on c.l.py indicated
that
    they preferred lambda() because it limited the size of anonymous
functions.
    The PEP author agrees with them, in general.  This is one of the
reasons
    why the word 'def' needs to be on the line after the opening
parenthesis.

    There was a discussion about whether this affected the syntax for
lists,
    tuples and dictionaries. It does not.


References

    See the discussion on c.l.py beginning in late July, 2002, titled:
Let's
    Talk About Lambda Functions.


Acknowledgments:
    I gratefully acknowledge the help of Fredrick Lundh for the initial
idea
    of using the existing statement level def syntax, Bengt Richter for
    cutting through the fog by suggesting the use of parenthesis to
enclose
    the definitions, and Greg Ewing for clarifying what it is that makes
    a function a method.  Also everyone else who contributed to the
discussion
    and helped sharpen the focus of the idea.


Copyright

    This document has been placed in the public domain.



Local Variables:
mode: indented-text
indent-tabs-mode: nil
sentence-end-double-space: t
fill-column: 70
End:








More information about the Python-list mailing list