From 976c70467706c4415b75eeecf5864740bb28b9bb Mon Sep 17 00:00:00 2001 From: Collin Winter Date: Tue, 1 May 2007 18:31:51 +0000 Subject: [PATCH] Add PEP 3129, 'Class Decorators'. --- pep-0000.txt | 2 + pep-3129.txt | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 pep-3129.txt diff --git a/pep-0000.txt b/pep-0000.txt index 3c19e04ca..8e4bc5813 100644 --- a/pep-0000.txt +++ b/pep-0000.txt @@ -127,6 +127,7 @@ Index by Category S 3126 Remove Implicit String Concatenation Jewett S 3127 Integer Literal Support and Syntax Maupin S 3128 BList: A Faster List-like Type Stutzbach + S 3129 Class Decorators Winter S 3141 A Type Hierarchy for Numbers Yasskin Finished PEPs (done, implemented in Subversion) @@ -496,6 +497,7 @@ Numerical Index S 3126 Remove Implicit String Concatenation Jewett S 3127 Integer Literal Support and Syntax Maupin S 3128 BList: A Faster List-like Type Stutzbach + S 3129 Class Decorators Winter S 3141 A Type Hierarchy for Numbers Yasskin diff --git a/pep-3129.txt b/pep-3129.txt new file mode 100644 index 000000000..e996512cd --- /dev/null +++ b/pep-3129.txt @@ -0,0 +1,117 @@ +PEP: 3129 +Title: Class Decorators +Version: $Revision: 53815 $ +Last-Modified: $Date: 2007-04-27 16:42:06 -0700 (Fri, 27 Apr 2007) $ +Author: Collin Winter +Status: Draft +Type: Standards Track +Content-Type: text/x-rst +Created: 1-May-2007 +Python-Version: 3.0 +Post-History: + + +Abstract +======== + +This PEP proposes class decorators, an extension to the function +and method decorators introduced in PEP 318. + + +Rationale +========= + +When function decorators were originally debated for inclusion in +Python 2.4, class decorators were seen as obscure and unnecessary +[#obscure]_ thanks to metaclasses. After several years' experience +with the Python 2.4.x series of releases and an increasing +familiarity with function decorators and their uses, the BDFL and +the community re-evaluated class decorators and recommended their +inclusion in Python 3.0 [#approval]_. + +The motivating use-case was to make certain constructs more easily +expressed and less reliant on implementation details of the CPython +interpreter. While it is possible to express class decorator-like +functionality using metaclasses, the results are generally +unpleasant and the implementation highly fragile [#motivation]_. The +fact that large-scale Python projects like Zope were going through +these wild contortions to achieve something like class decorators won +over the BDFL. + + +Semantics +========= + +The semantics and design goals of class decorators are the same as +for function decorators ([#semantics]_, [#goals]_); the only +difference is that you're decorating a class instead of a function. +The following two snippets are semantically identical: :: + + class A: + pass + A = foo(bar(A)) + + + @foo + @bar + class A: + pass + +For a detailed examination of decorators, please refer to PEP 318. + + +Implementation +============== + +The grammar for class declarations changes from :: + + classdef: 'class' NAME ['(' [testlist] ')'] ':' suite + +to :: + + classdef: [decorators] 'class' NAME ['(' [testlist] ')'] ':' suite + +The Python AST and bytecode must be modified accordingly. + +A reference implementation [#implementation]_ has been provided by +Jack Diederich. + + +References +========== + +.. [#obscure] + http://www.python.org/dev/peps/pep-0318/#motivation + +.. [#approval] + http://mail.python.org/pipermail/python-dev/2006-March/062942.html + +.. [#motivation] + http://mail.python.org/pipermail/python-dev/2006-March/062888.html + +.. [#semantics] + http://www.python.org/dev/peps/pep-0318/#current-syntax + +.. [#goals] + http://www.python.org/dev/peps/pep-0318/#design-goals + +.. [#implementation] + http://python.org/sf/1671208 + + + +Copyright +========= + +This document has been placed in the public domain. + + + +.. + Local Variables: + mode: indented-text + indent-tabs-mode: nil + sentence-end-double-space: t + fill-column: 70 + coding: utf-8 + End: