[Python-Dev] Class decorators

Phillip J. Eby pje at telecommunity.com
Wed Mar 29 07:01:55 CEST 2006


At 11:35 PM 3/28/2006 -0500, Fred L. Drake, Jr. wrote:
>For Zope 3, we have decorators that work with the component architecture (I'm
>sure Phillip is familiar with these).  They're used with functions to
>indicate that the function adapts a particular kind of object, or that it
>implements or provides a particular interface.  We have different functions
>that get used for this purpose in classes that are executed within the body
>of the class.  There's some merit to being able to use a single set of
>functions in both cases, since the use cases are the same.  I'm not sure I'd
>want to change the existing pattern, though, since it's already so widespread
>within the Zope 3 codebase (including 3rd-party components).

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
         """

And it only gets worse when you have lots of arguments or multiple decorators.

For some reason, this doesn't bother me with functions.  But then, I can't 
remember how often I've actually needed to use two decorators on the same 
function, or how many times a function decorator's arguments took multiple 
lines to list.  Both of these are routine occurrences for my class use cases.

It's too bad this syntax is ambiguous:

     class Foo:
         """Docstring here, blah blah blah
         """
         @implements(IFoo)

As this achieves a desirable highlighting of the specialness, without 
forcing the decorator outside the class.  Oh well.



More information about the Python-Dev mailing list