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. The metaclass I wrote was for the purpose of adding a class to a registry, the reason for which isn't important here. It worked, but I was surprised to find that it not only registered the classes that I made it the metaclass of, but all subclasses of those classes as well. I'm not sure whether that's really the behaviour I want, and I can imagine some cases in which it's definitely not what I'd want. The general principle brought out here is that when you use a metaclass, it gets inherited by subclasses, but if we had class decorators, they would only affect to the classes that you explicitly applied them to. I think there are uses for both behaviours. Greg
Greg Ewing
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. http://lists.ironpython.com/pipermail/users-ironpython.com/2006-March/002007... Mike
At 07:20 PM 3/27/2006 +0000, Mike Krell wrote:
Greg Ewing
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.
http://lists.ironpython.com/pipermail/users-ironpython.com/2006-March/002007...
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 def some_advisor(whatever_args): def callback(cls): print "I can modify",cls,"or replace it" return cls addClassAdvisor(callback) you can then use it in a class body like so: class SomeClass: some_advisor("something") 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. ;)
On 3/27/06, Phillip J. Eby
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
def some_advisor(whatever_args): def callback(cls): print "I can modify",cls,"or replace it" return cls addClassAdvisor(callback)
you can then use it in a class body like so:
class SomeClass: some_advisor("something")
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. ;)
Just curious (and lazy): what magic is the implementation using that makes this work without a custom metaclass? -- --Guido van Rossum (home page: http://www.python.org/~guido/)
Mike Krell wrote:
Greg Ewing
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.
http://lists.ironpython.com/pipermail/users-ironpython.com/2006-March/002007...
the same is true for Jython and Java annotations.
On 3/28/06, Samuele Pedroni
Mike Krell wrote:
Greg Ewing
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.
http://lists.ironpython.com/pipermail/users-ironpython.com/2006-March/002007...
the same is true for Jython and Java annotations.
OK, I'm convinced (mostly by the awful hackery that Phillip so proudly exposed :-). I propose that someone start writing a Py3k PEP for class decorators. I don't think it's fair to the 2.5 release team to want to push this into 2.5 though; how about 2.6? -- --Guido van Rossum (home page: http://www.python.org/~guido/)
On 3/28/06, Guido van Rossum
I propose that someone start writing a Py3k PEP for class decorators. I don't think it's fair to the 2.5 release team to want to push this into 2.5 though; how about 2.6?
Wasn't there already a (pretty small) patch? I guess it would be different now with the AST though. n
On Tue, Mar 28, 2006 at 10:16:01AM -0800, Neal Norwitz wrote:
On 3/28/06, Guido van Rossum
wrote: I propose that someone start writing a Py3k PEP for class decorators. I don't think it's fair to the 2.5 release team to want to push this into 2.5 though; how about 2.6?
Wasn't there already a (pretty small) patch? I guess it would be different now with the AST though.
I submitted one a couple years ago. The AST makes it obsolete though. I'd be happy to make a new AST friendly one (time to learn the AST..) -Jack
participants (7)
-
Greg Ewing
-
Guido van Rossum
-
Jack Diederich
-
Mike Krell
-
Neal Norwitz
-
Phillip J. Eby
-
Samuele Pedroni