Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's not so much "lines" that are a problem; it's that lambdas cannot contain any statements.

For example, here's a multi-line lambda, containing only expressions:

    >>> lambda f, pred, xs: [
    ...   f(x) for ys in xs
    ...        for x  in ys
    ...        if pred(x)
    ... ]
    <function <lambda> at 0x1113e80d0>
Here's a one-line lambda, attempting to use a statement:

    >>> lambda x: (x += 1)
      File "<stdin>", line 1
        lambda x: (x += 1)
                     ^
    SyntaxError: invalid syntax
Note that the parentheses are to avoid parsing a '+=' expression with 'lambda x: x' on the left and '1' on the right:

    >>> lambda x: x += 1
      File "<stdin>", line 1
    SyntaxError: cannot assign to lambda
This restriction is problematic because of the following combination:

- Python statements don't compose as well as expressions; we can write one statement followed by another, and we can nest statements (or blocks) inside control statements (if, else, with, etc.), but that's about it. We can't assign statements to variables, we can't call a function on a statement, or return a statement from a function, etc. i.e. we can't abstract over statements (unless we try wrapping them in a named function and using that as an expression, but that can break the underlying functionality of the statement, e.g. 'def ifThenElse(cond, true, false):' will evaluate both branches when called).

- Python uses statements for a whole bunch of stuff. In particular it has a 'with foo as bar: baz' statement, rather than e.g. a 'with(foo, lambda bar: baz)' expression; the recent pattern matching functionality is a statement rather than an expresssion; defining a named function is a statement; etc.

Taken together, we can often find ourselves needing to introduce a statement somewhere in our code; that requires us to turn a 'lambda' into a 'def'; but 'def' is a statement, so we have to turn any enclosing expression into a statement too; and this propagates up to disintegrate whatever expression we had; leaving us with a big pile of statements, full of boilerplate for managing scopes and names.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: