[Python-Dev] @decorator syntax is sugar, but for what exactly?

James Y Knight foom at fuhm.net
Tue Aug 10 04:48:37 CEST 2004


On Aug 7, 2004, at 7:51 PM, Bengt Richter wrote:
> ISTM that
>     @limited_expression_producing_function
>     @another
>     def func(): pass
>
> is syntactic sugar for creating a hidden list of functions. (Using '|' 
> in place of '@'
> doesn't change the picture much (except for people whose tools depend 
> on '@' ;-)).


As I understand it, it's really syntax sugar for:
     func = limited_expression_producing_function(another(
         defbutdontbind func(): pass))

except that isn't actually valid python source code. ;) It's nearly, 
but not exactly, the same as
     def func(): pass
     func = another(func)
     func = limited_expression_producing_function(func)


> Is this a special case of a more general idea? E.g., could it apply to
> right after ANY next name is bound, in general, not just a name bound 
> by def?

Right now decorators only work before def.

I posted an idea earlier about a @public decorator, and wrote a quick 
proof-of-concept patch to allow decorators in front of funcdef, 
classdef, and "NAME '=' testlist". This much *can* work with little 
code change, if the functionality is deemed as desirable.

However, as I mentioned, @public in front of a arbitrary binding 
expression (e.g. not class or function) isn't useful unless the 
decorator gets the name of the variable being bound. I didn't quite 
figure out how to manage that part. :)

James



More information about the Python-Dev mailing list