Multi-line lambda proposal.

Sybren Stuvel sybrenUSE at YOURthirdtower.com.imagination
Wed May 10 05:01:42 EDT 2006


Kaz Kylheku enlightened us with:
> In the case of if/elif/else, they have to be placed behind the
> closest suite that follows the expression in the syntax of the
> statement:
>
>   if lambda(x)(4) < 0:
>      print "a"
>   lambda:
>      return x + 1
>   elif y = 4:
>      print "b"
>   else:
>      print "foo"
>
> The overall rule is simple and uniform: each suite can have lambda:
> clauses. These have to match lambda() occurences in the expression
> (or expression list) immediately to the left in the same grammar
> production.

IMHO I think it's ugly and unreadable. If you use it in such a way, a
much more elegant way would be to use an inner function:

    def increment(x):
        return x + 1

    if increment(4) < 0:
       print "a"
    elif y = 4:
       print "b"
    else:
       print "foo"

This also solves the readability issue when there are multiple
multi-line lambdas involved. Just write a couple of inner functions
neatly under each other, and call them by name. From my point of view,
that's much more readable, and if there is any error, it'll be much
easier to find due to proper tracebacks.

> On the other hand, some people might find this compromise more
> attractive: simply do not allow multi-line lambdas in compound
> statements.

I'm one of those people. I don't like multi-line lambdas at all,
actually.

Sybren
-- 
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself? 
                                             Frank Zappa



More information about the Python-list mailing list