[Python-Dev] Class decorators

Ian Bicking ianb at colorstudy.com
Thu Mar 30 19:14:36 CEST 2006


Fred L. Drake, Jr. wrote:
>  > 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.
> 
> Agreed, but... guess we can't have everything.  On the other hand, something 
> like:
> 
>     class Foo:
>         """Documentation is good."""
> 
>         @class implements(IFoo)
> 
> is not ambiguous.  Hmm.  It even says what it means.  :-)

This is quite reminiscent of Ruby to me, where:

   class Foo:
       implements(IFoo)

basically means:

   class Foo:
       pass
   Foo.implements(IFoo)

For a variety of reasons that doesn't work for Python, but what you 
propose accomplishes the same basic thing.

I'm coming in a little late on all this, but I find moving the decorator 
inside the class statement to be a substantial improvement, even if it 
is also a trivial improvement ;)  Anytime I've done thought experiments 
about using class decorators, the results is very hard to read.  That 
classes are inherently declarative and open, while functions are 
imperative and closed, makes the constructs very different.


-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org


More information about the Python-Dev mailing list