[Python-ideas] Anonymous functions for decorators

Adam Atlas adam at atlas.st
Wed Dec 26 01:03:05 CET 2007


I don't think def temp(...) is so terrible. Perhaps it's slightly  
inelegant, but adding a new keyword just so decorator syntax can be  
used in a decidedly non-decoratorish way to save a small amount of  
typing doesn't seem like something that would be accepted. If we were  
adding anonymous functions, I should think we'd make them more  
powerful while we're at it; it would at least be good to have them be  
first-class expressions, so you could do:

DECORATOR(def (...args...): ...function...) #or whatever the syntax  
for anonymous functions would end up being

and all the other useful things that come with anonymous functions and  
closures.

I think I suggested a while ago that we allow that syntax -- def  
(args): ...statements... -- to be used as an expression, and also  
allow it to span multiple lines like a normal Python function  
definitions, but I was told that this would complicate parsing of  
indentation. I'm not sure why that is, but I'm not familiar with the  
internals of Python's parser.

In any case, you can currently do
@DECORATOR
def temp(...args...):
     ...function...

(as you are already doing in your Twisted example) and all you'll end  
up with is a name called "temp" bound to None. Not too bad. I really  
don't think inhibiting the assignment of some Nones is worth a new  
keyword and such.


On 25 Dec 2007, at 18:33, Ryan Freckleton wrote:
> In general:
>
> @DECORATOR
> do (..args...):
>    ..function
>
> would be the same as:
>
> def temp(..args..):
>    ..function
> DECORATOR(temp)
> del temp



More information about the Python-ideas mailing list