[Python-Dev] @decoration of classes

Jp Calderone exarkun at divmod.com
Sat Mar 26 23:16:49 CET 2005


On Sat, 26 Mar 2005 22:49:33 +0100, Eric Nieuwland <eric.nieuwland at xs4all.nl> wrote:
>On 26 mrt 2005, at 21:36, Josiah Carlson wrote:
> > Eric Nieuwland <eric.nieuwland at xs4all.nl> wrote:
> >> Given the ideas so far, would it possible to:
> >>
> >> def meta(cls):
> >> 	...
> >>
> >> @meta
> >> class X(...):
> >> 	...
> >
> > It is not implemented in Python 2.4.  From what I understand, making it
> > happen in Python 2.5 would not be terribly difficult.  The question is
> > about a "compelling use case".  Is there a use where this syntax is
> > significantly better, easier, etc., than an equivalent metaclass?  
> > Would
> > people use the above syntax if it were available?
> >
> > What would you use the above syntax to do?
> 
> Well, I can imagine using
> 
> @meta(MyMetaClass)
> class MyClass(...):
> 	...
> 
> instead of
> 
> class MyClass(...):
> 	__metaclass__ = MyMetaClass
> 	...
> 
> Somehow, it seems more aesthetic to me.

  This doesn't quite work the same, though.  The former creates a new instance of ClassType, then (presumably) rips it apart and passes the pieces to MyMetaClass.

  The latter just passes the pieces to MyMetaClass unassembled.

  I can imagine cases where the class creation would fail during the first step of the former process, so I don't think this is actually a use-case for class decorators.

  Jp


More information about the Python-Dev mailing list