Deferred expressions (was Re: Lambda as declarative idiom)
ncoghlan at iinet.net.au
Thu Jan 6 06:58:46 EST 2005
Bengt Richter wrote:
> I like the fact that 'def' can serve as a mnemonic for 'defer' or 'deferred' ;-)
Yeah, me too. I didn't actually notice that until after I'd thought of the phrase.
> OTOH, I like concise notation for expressions, and the def and from aren't
> really necessary if you can tag the first expression with some syntax marker.
> I suggested (: rather than (def since (: is illegal now and won't break anything.
True, but I've always liked Python's preference for keywords over punctuation.
And 'def' is only 2 characters longer than ':', too.
> That is an advantage of having it inside the outer parens, which my (:expr)(params)
> couldn't benefit from -- unless I just take your format and substitute ':' for
> both def and from:
But where does that leave "Python is executable pseudocode"?
(lambda (a, b, c) : f(a) + o(b) - o(c))
(: f(a) + o(b) - o(c) : (a, b, c))
(def f(a) + o(b) - o(c) from (a, b, c))
Which of the above most clearly expresses "defer a functional expression taking
arguments a, b, and c"?
For comparison, named syntax requires 3 lines, since Guido also wants to remove
suite one-liners in Py3k:
def f1(a, b, c):
return f(a) + o(b) - o(c)
Not so bad for a single deferred expression (particularly a
not-completely-trivial one like this), but fares significantly worse for simple
zero-argument functions, or if you are just setting up a couple of deferred
expressions in a function call. There's no more virtue in completely squandering
vertical screen real estate than there is in being overly conservative with it.
> What was the point of all this again? Pretending lambda goes away without really killing it? ;-)
Actually, it was started by the "please don't take our lamdbas!" comments
regarding Python 3k. I suggested that one of the problems with lambda is that it
is an expression but looks like a one-liner statement ("it's ugly as sin" may
have been closer to my actual phrasing), that the syntax would be deservedly
rejected if proposed as a new addition to the language, and a more productive
response might be to consider nicer alternative syntax that Guido would consider
retaining in Python 3k (since it would make sense to try out such a syntax in
the Python 2.x series before committing to it for 3.0).
Of course, after making that suggestion, my brain decided to start working on
its *own* ideas for a more Pythonic syntax (trying to learn from generator
expressions), which seemed to set off a few other people :)
The name 'deferred expression' is intended to emphasise what I see as the most
appropriate application for anonymous functions - using an entire named function
statement to defer a simple expression is serious overkill. Recognising this use
case was what switched me from "I won't miss lambdas" to "You know, I'd really
regret it if they disappeared completely".
Basically, I think lambda has a PR problem - its usefulness as a way to defer
expression evaluation is obscured by its ugly syntax and its current explanation
as "a neutered form of function definition, but at least you can use it in an
On backticks, they have a major problem in that many fonts make ` and '
virtually indistinguishable. A secondary problem is that printed text often
renders an opening single quote like a backtick. Guido has already mentioned
this in one of Python Regrets talks (in the context of the backtick repr
syntax). So I don't expect any syntax involving backticks to even be looked at
by the BDFL.
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
More information about the Python-list