[Python-ideas] YAML (yet-another-multiline-lambda)

Antony Lee antony.lee at berkeley.edu
Tue Oct 22 12:10:29 CEST 2013


As the issue has been (indirectly) raised again in the recent context
manager thread, I would like to propose yet another syntax for multiline
lambdas...  I know there has been many but I (foolishly?) hope this one is
simpler.  I do not wish to focus on the usual arguments for and against the
usefulness of such a construct (from the archives of the list, I feel that
the lack of an attractive syntax is the bigger barrier).

Specifically, I suggest that the "def" (and possibly the "class")
keyword(s) may be used in an expression context, if immediately surrounded
by parentheses.  The indentation of the body of the function is given by
the indentation of the first line after the def.

target = (def f(arg):
    <...>)

add_callback(
    def callback(arg):
        <...>)

callbacks = {
    "foo":
        (def foo():
            <...>), # note how the parentheses disambiguate where the last
comma belongs
    "bar":
        (def bar():
            <...>)
}

As an extra suggestion, one may want to allow not specifying a function
name in these contexts:

callbacks = {
    "foo":
        (def ():
            <...>)
}

(in which case the function would be named "<lambda>", of course) but I do
not think that this is a critical feature (and it could always be added
later).

Feel free to critique,

Antony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131022/a6a8df9a/attachment.html>


More information about the Python-ideas mailing list