[Python-Dev] Definining properties - a use case for class decorators?

Josiah Carlson jcarlson at uci.edu
Mon Oct 24 12:09:07 CEST 2005

Nick Coghlan <ncoghlan at gmail.com> wrote:
> Josiah Carlson wrote:
> > You can get the same semantics with...
> > 
> > class NAME(_(TYPE), ARGS):
> >     BLOCK
> > 
> > And a suitably defined _.  Remember, not every X line function should be
> > made a builtin or syntax.
> And this would be an extremely fragile hack that is entirely dependent on the 
> murky rules regarding how Python chooses the metaclass for the newly created 
> class. Ensuring that the metaclass of the class returned by "_" was always the 
> one chosen would be tricky at best and impossible at worst.

The rules for which metaclass is used is listed in the metaclass
documentation.  I personally never claimed it was perfect, and neither
is this one...

class NAME(_(TYPE, ARGS)):

But it does solve the problem without needing syntax (and fixes any
possible metaclass order choices).

> Even if it *could* be done, I'd never want to see a hack like that in 
> production code I had anything to do with.

That's perfectly reasonable.

> (I put the metaclass after the keyword, because, unlike a function decorator, 
> the metaclass is invoked *before* the class is created, and because you're 
> only allowed one explicit metaclass)

Perhaps, but because the metaclass can return anything (in this case, it
returns a property), being able to modify the object that is created may
be desireable...at which point, we may as well get class decorators for
the built-in chaining.

 - Josiah

More information about the Python-Dev mailing list