[Python-ideas] a new lambda syntax

Steven D'Aprano steve at pearwood.info
Fri Oct 23 04:30:46 CEST 2009


On Thu, 22 Oct 2009 07:49:14 pm Sturla Molden wrote:

> If that is not allowed then, an anynymous block cannot be called or
> referenced, as there are no name associated with it.

That is clearly wrong. You don't need a name to call an object. You just 
need the object.

>>> (lambda x: x+1)(42)
43
>>> [None, lambda s: s.upper(), None][1]('abc')
'ABC'



> So to be useful, 
> it would always have to be associated with a decorator.  That would
> be the only way to get access to it:
>
> @decorator
> def():
>     <suite>

That doesn't even make sense to me. I don't understand what the 
decorator is doing there, or how it helps you access the anonymous 
function. Under today's semantics (modified to allow anonymous def), 
that would create an anonymous function containing <suite>, then pass 
that function to decorator(), and treat the return result as anonymous. 
Since you're not keeping a reference to the anonymous function, it will 
then be garbage collected. Passing it into a decorator first won't 
change that (unless the decorator stores the function somewhere as a 
side-effect).

In other words, the above is just like this:

decorator(lambda: <suite>)

which is legal but pointless unless you do something with the result.



> Now we can see that the use for this anonymous block "def()" simply
> goes away, as it too is redundant syntax.

Why? Functions aren't the only thing you can apply decorators to. You 
can also call:

@decorator
class K:
    pass

in Python 2.6 and better.



-- 
Steven D'Aprano



More information about the Python-ideas mailing list