[Python-Dev] Class decorators

Phillip J. Eby pje at telecommunity.com
Thu Mar 30 02:23:03 CEST 2006


At 11:07 AM 3/29/2006 -0800, Guido van Rossum wrote:
>On 3/28/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> > If we're using Zope 3 as an example, I personally find that:
> >
> >      class Foo:
> >          """Docstring here, blah blah blah
> >          """
> >          implements(IFoo)
> >
> > is easier to read than:
> >
> >      @implements(IFoo)
> >      class Foo:
> >          """Docstring here, blah blah blah
> >          """
>
>But the former also smells more of magic.

My comment above was only about readable *placement* of the decorators, not 
the actual syntax.  Many approaches to the actual syntax in the body are 
possible.

For example, what did you think of Fred Drakes's "@class" proposal?  To 
specify it formally, one could say that this:

     @class EXPR

in a class scope would expand to the equivalent of:

     locals().setdefault('__decorators__',[]).append(EXPR)

and is a syntax error if placed anywhere else.  That, combined with support 
for processing __decorators__ at class creation time, would fulfill the 
desired semantics without any implicit "magic".

(The locals() part could of course be implemented in bytecode as 
LOAD_LOCALS, since class scopes implement their locals as a 
dictionary.  That would avoid the need for adding any new bytecodes, since 
this isn't a performance-sensitive feature.)



More information about the Python-Dev mailing list