[Python-Dev] pep 318, Decorators for Functions, Methods and Classes

Shane Holloway (IEEE) shane.holloway at ieee.org
Fri Aug 6 19:07:22 CEST 2004


Guido van Rossum wrote:
 >>It doesn't seem to work, instead it issues a SyntaxError
 >>on the class keyword. Also, the discussion is completely
 >>focused on decorating functions and methods.
 >
 >
 > Some people have argued that it should work for classes (Java and C#
 > decorators work for any declaration).  I've never been convinced,
 > although I'm not dead set against it either.
 >
 > It would seem that the functionality isn't quite as useful there; you
 > can get most of the same effects with metaclasses.  I'm not aware of
 > any idiom that takes a class and passes it through some kind of
 > wrapper or transformation *after* the class declaration; if somebody
 > is using this, it surely must be a very rare use indeed.
 >
 > --Guido van Rossum (home page: http://www.python.org/~guido/)

I have done a few things in the realm of Aspect Oriented programming, 
replacing __metaclass__ with a callable that delegates the "hard work" 
to type() and then plays with the resulting class rather like 
classmethod does.  I remember trying the replace semantics after the end 
of the class, but preferred the in-the-face-"pay-attention" of using the 
metaclass hook.  I think that class pie-decorators would serve the same 
purpose.

I could see the protocols "implements" functionality expressed through 
class decorators, although what they have now works fine.  On the 
positive side, decorators for classes will probably be significantly 
easier to understand and implement than metaclasses, though less powerful.

So, my thoughts on class decorators are:
1) They have their purposes, and direct support will probably bring out 
more uses of them
2) Orthogonality is good -- the same syntax for methods and classes 
would be preferred, and understanding would transfer between the two forms
3) They are not needed right now -- lets just make sure they can work in 
the same way as method decorators if we want them later


And for the record, I prefer the @-syntax.  Readable, short, easily 
commentable, multi-line, and before the function declaration.  I think 
it will be a nice addition.  Though other characters would be fine, too. 
  I'm really tired of typing my method names three times with the 2.3 
syntax. ;)

Thanks for your time,
-Shane Holloway


More information about the Python-Dev mailing list