[Python-3000] Possible alternative lambda syntax?

Adam Atlas adam at atlas.st
Sat Jan 13 08:23:05 CET 2007


I've seen <http://mail.python.org/pipermail/python-dev/2006-February/ 
060415.html> and the various discussions about this, so I hope I'm  
not beating an utterly dead horse here, so if I am, please ignore  
this... but I've been thinking about this issue and I had a couple of  
ideas that I wanted to air.

The first is inspired a little bit by Python 2.4's generator  
expressions. Imagine this as the syntax: (a, b: a+b). I find that to  
be quite simple and Pythonic. I don't see it having been proposed by  
anyone on the wiki's AlternateLambdaSyntax page, but it seems pretty  
intuitive. Certainly better than some of the odd proposals on that  
page like (a + b from args(a, b)).

The second is an interesting idea for a syntax abstraction... imagine  
if the 'def' keyword and the syntax for creating a function were  
decoupled. def x y would assign the value y to the name x in the  
local namespace, and, if y has a __name__ property, assign x to it.  
The value b could technically be anything, not just a function.  
Meanwhile, the syntax for constructing a function object would be  
such that plain function definitions would look the same as they do  
now, but it would also be usable elsewhere. The syntax would be  
(arglist): statements. Where there's no ambiguity (such as after a  
'def' or in parentheses), it could be multi-line as usual.  
Advantages: Lambdas and normal functions would be one in the same  
(internally); what we now call a lambda would be defined like ((a,  
b): return a+b), which would be taken just like any other function  
but without its __name__ defined. Not as concise for lambdas as my  
first idea, but it's more consistent all around, and you can still  
easily see what's going on. It could also be used to pass whole  
multiline functions anonymously as arguments, e.g.:

     doSomething((a, b):
         print a, b
         return a+b
     )

That way, there's no need for adding braces or anything unthinkable  
like that. When it's somewhere more complex, like between other  
arguments to a function, or somewhere where its arglist could be  
misinterpreted as a tuple, just enclose the whole construction in  
parentheses to remove any ambiguity. That way the parsing could  
remain simple.

Any thoughts on either of these?


More information about the Python-3000 mailing list