[Python-Dev] Class decorators
Phillip J. Eby
pje at telecommunity.com
Tue Mar 28 04:23:50 CEST 2006
At 07:20 PM 3/27/2006 +0000, Mike Krell wrote:
>Greg Ewing <greg.ewing <at> canterbury.ac.nz> writes:
> > I've just been playing around with metaclasses, and
> > I think I've stumbled across a reason for having
> > class decorators as an alternative to metaclasses
> > for some purposes.
>There has also been discussion on the IronPython mailing list that class
>decorators would be a very useful syntax for expressing .NET attributes.
PyProtocols and the zope.interface package both support inline class
decorators called "class advisors". They don't require any special syntax,
and aren't much more complex than regular decorators. By defining an
advisor like this:
from protocols.advice import addClassAdvisor
print "I can modify",cls,"or replace it"
you can then use it in a class body like so:
And the return value from 'callback' will replace SomeClass, just like a
decorator replaces the function it's called on.
The implementation should work with any Python version from 2.2 up. I'm
not sure if it would work with IronPython. But if it doesn't, that would
be a good indication of feature(s) that IronPython is missing. ;)
More information about the Python-Dev