[Python-3000] PEP Draft: Class Decorators

Jack Diederich jackdied at jackdied.com
Wed Feb 28 21:52:12 CET 2007


Greetings from PyCon!

I read hundreds of emails in the dozens of threads about class decorators and
there was surprisingly little content (most of the arguments were about syntax
which is no longer up for debate). As a result this PEP is quite plain.

If any IronPython or Jython folks could throw in their two bits it would be
appreciated.  

PEP: 3XXX
Title: Class Decorators
Version: 1
Last-Modified: 28-Feb-2007
Authors: Jack Diederich
Implementation: SF#1671208
Status: Draft
Type: Standards Track
Created: 26-Feb-2007

Abstract
========

Extending the decorator syntax to allow the decoration of classes.

Rationale
=========

Allowing classes to be decorated serves many of the same purposes as
the ability to decorate functions.  Decorators move factory registration
and class manipulation to the top of the class definition instead of the
current alternate methods of post-processing or the action-at-a-distance
of metaclasses.

    import myfactory
      
    @myfactory.register
    class MyClass:
        pass

History and Implementation
==========================

Class decorators were originally proposed in PEP318 [1]_ and were rejected
by Guido [2]_ for lack of use cases.  Two years later he saw a use case
he liked and gave the go-ahead for a PEP and patch [3]_.

The current patch is loosely based on a pre-2.4 patch [4]_ and updated to
use the new AST.

Grammar/Grammar is changed from

   funcdef: [decorators] 'def' NAME parameters ['->' test] ':' suite

to

    decorated_thing: decorators (classdef | funcdef)
    funcdef: 'def' NAME parameters ['->' test] ':' suite

References
==========
If you enjoyed this PEP you might also enjoy:

.. [1] PEP 318, "Decorators for Functions and Methods"
  http://www.python.org/dev/peps/pep-0318/

.. [2] Class decorators rejection
  http://mail.python.org/pipermail/python-dev/2004-March/043458.html

.. [3] Class decorator go-ahead
  http://mail.python.org/pipermail/python-dev/2006-March/062942.html

.. [4] 2.4 class decorator patch
  http://python.org/sf/1007991

.. [5] 3.x class decorator patch
  http://python.org/sf/1671208



More information about the Python-3000 mailing list