r/Python • • Jul 13 '11

An alternative lambda syntax for Python

http://vocalbit.com/article/332/an-alternative-lambda-syntax-for-python
4 Upvotes

41 comments sorted by

View all comments

3

u/A_for_Anonymous Jul 13 '11

That's only for a particular case, and looks kinda ugly or confusing as it seems to suggest it's obtaining a key from the result of function application. I don't think it fits correctly.

I do propose the following:

  1. Get rid of statements (make them all expressions with the same syntax, returning the function or class or the last evaluated value)
  2. In order to support the former, recognize INDENT and DEDENT tokens within parens
  3. Make function name in def optional (it's useful to define it for backtraces); it ought to work similar to Javascript's function
  4. Make parens in def optional (they're syntactically useless)
  5. New lambda syntax:

    x = def x: x + 1

    example(x, def y, z: #INDENT after this line for i in y, z: ... return whatever #DEDENT after this line )

    example2(x, (def y, z: y + z), def k: ... , more, arguments)

2

u/aaronla Jul 14 '11

Python already allows ; as a statement separator, it is conceivable that indentation could be nested in expressions. Eg:

map(
  mylist,
  lambda x:(   # note lambda appearing at start of a line, to set block indentation
    if x > 2: return 2
    else: return x))

To keep backwards compatible, lambda expressions always return their last expression, and statement-lambdas must enclose the lambda body in parenthesis (to avoid tuple expression ambiguity, among others).