Securing a future for anonymous functions in Python

John Roth newsgroups at jhrothjr.com
Thu Dec 30 09:39:54 EST 2004


"Nick Coghlan" <ncoghlan at iinet.net.au> wrote in message 
news:mailman.8597.1104413330.5135.python-list at python.org...
> GvR has commented that he want to get rid of the lambda keyword for Python 
> 3.0. Getting rid of lambda seems like a worthy goal, but I'd prefer to see 
> it dropped in favour of a different syntax, rather than completely losing 
> the ability to have anonymous functions.
>
> Anyway, I'm looking for feedback on a def-based syntax that came up in a 
> recent c.l.p discussion:
> http://boredomandlaziness.skystorm.net/2004/12/anonymous-functions-in-python.html
>
> Cheers,
> Nick.

I think it's rather baroque, and I agree with Paul Ruben
that it needs multiple statement capability.

The syntax I prefer (and I don't know if it's actually been
suggested before) is to use braces, that is { and }.

In other words, an anonymous function looks like:
    {p1, p2, p3 |
      stmt1
      stmt2
     }

There are two reasons for using braces. One is
that it's the common syntax for blocks in a large number
of languages. The other is that it should be relatively
easy to disambiguate from dictionary literals, which are
the only other current use of braces.

The parameter list is optional, as is the bar ending
the list. The reason for the bar instead of a colon
is to help the parser in the case of a single parameter,
which would look like the beginning of a dictionary
literal. If the parser doesn't need the help, then a colon
would be more consistent,  hence better.

A second issue is indentation. I'd set the indentation
boundary wherever the _second_ line of the construct
starts, as long as it's to the right of the prior
indentation boundary. The unfortunate part of this, and one of the
major stumbling blocks, is that it might take some
significant reconceptualizing of the lexer and parser,
which currently isn't set up to shift from expression
back to statement mode and then return to expression
mode.

John Roth

> -- 
> Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
> ---------------------------------------------------------------
>             http://boredomandlaziness.skystorm.net 




More information about the Python-list mailing list