[Python-Dev] Class decorators

Phillip J. Eby pje at telecommunity.com
Tue Mar 28 21:41:52 CEST 2006


At 11:04 AM 3/28/2006 -0800, Guido van Rossum wrote:
>That's fine. But there's also the C#/Java POV.

Can someone point me to examples of C# class "attributes" and Java 
annotations that they'd like to use with this mechanism?  I would indeed 
like to see how those use cases compare with mine.


>  I'm somehow concerned
>that any mechanism that puts the syntax inside the class body is
>somehow going to have a hackish implementation,

That depends on whether there's syntax support, or it's just something that 
happens at runtime.  I'm not opposed to having syntax, I just don't think 
that putting the syntax outside the class is the best thing for most of my 
class decorator use cases.

Nonetheless, neither special syntax nor hackiness is really required.  For 
example, your proposal here:

     http://mail.python.org/pipermail/python-dev/2004-April/043913.html

was to use a '__decorators__' attribute, which would then be processed by a 
metaclass.  However, this could simply become a standard magic attribute 
ala __metaclass__, and be processed by the class creation machinery after 
first invoking the correct metaclass.

I don't really *like* that approach, but it requires neither syntax nor 
hacks, (nor a custom metaclass) and it could be syntax-sugared later.

For that matter, if this __decorators__ machinery existed, the existing 
advisor implementation could become a lot less hacky.  The only magical bit 
at that point would be doing something like:

      locals = sys._getframe(n).f_locals.
      locals.setdefault('__decorators__',[]).append(decorator)

And this of course could go in a function in the stdlib, rather than have 
everybody writing their own _getframe() hacks.



More information about the Python-Dev mailing list