1
1
PEP: 520
2
- Title: Ordered Class Definition Namespace
2
+ Title: Preserving Class Attribute Definition Order
3
3
Version: $Revision$
4
4
Last-Modified: $Date$
5
5
Author: Eric Snow <
[email protected] >
@@ -8,7 +8,7 @@ Type: Standards Track
8
8
Content-Type: text/x-rst
9
9
Created: 7-Jun-2016
10
10
Python-Version: 3.6
11
- Post-History: 7-Jun-2016, 11-Jun-2016
11
+ Post-History: 7-Jun-2016, 11-Jun-2016, 20-Jun-2016
12
12
13
13
14
14
Abstract
@@ -20,44 +20,50 @@ namespace is copied into new ``dict`` and the original definition
20
20
namespace is discarded. The new copy is stored away as the class's
21
21
namespace and is exposed as ``__dict__`` through a read-only proxy.
22
22
23
- This PEP changes the default class definition namespace to ``OrderedDict``.
24
- The long-lived class namespace (``__dict__``) will remain a ``dict``.
25
- Furthermore, the order in which the attributes are defined in each class
26
- body will now be preserved in the ``__definition_order__`` attribute of
27
- the class. This allows introspection of the original definition order,
23
+ This PEP preserves the order in which the attributes in the definition
24
+ namespace were added to it, before that namespace is discarded. This
25
+ means it reflects the definition order of the class body. That order
26
+ will now be preserved in the ``__definition_order__`` attribute of the
27
+ class. This allows introspection of the original definition order,
28
28
e.g. by class decorators.
29
29
30
+ Additionally, this PEP changes the default class definition namespace
31
+ to ``OrderedDict``. The long-lived class namespace (``__dict__``) will
32
+ remain a ``dict``.
33
+
30
34
31
35
Motivation
32
36
==========
33
37
34
- Currently the namespace used during execution of a class body defaults
35
- to ``dict``. If the metaclass defines ``__prepare__()`` then the result
36
- of calling it is used. Thus, before this PEP, if you needed your class
37
- definition namespace to be ``OrderedDict`` you had to use a metaclass.
38
-
39
- Metaclasses introduce an extra level of complexity to code and in some
40
- cases (e.g. conflicts) are a problem. So reducing the need for them is
41
- worth doing when the opportunity presents itself. Given that we now have
42
- a C implementation of ``OrderedDict`` and that ``OrderedDict`` is the
43
- common use case for ``__prepare__()``, we have such an opportunity by
44
- defaulting to ``OrderedDict``.
45
-
46
- The usefulness of ``OrderedDict``-by-default is greatly increased if the
47
- definition order is directly introspectable on classes afterward,
48
- particularly by code that is independent of the original class definition.
38
+ Currently Python does not preserve the order in which attributes are
39
+ added to the class definition namespace. The namespace used during
40
+ execution of a class body defaults to ``dict``. If the metaclass
41
+ defines ``__prepare__()`` then the result of calling it is used. Thus,
42
+ before this PEP, to access your class definition namespace you must
43
+ use ``OrderedDict`` along with a metaclass. Then you must preserve the
44
+ definition order (from the ``OrderedDict``) yourself. This has a
45
+ couple of problems.
46
+
47
+ First, it requires the use of a metaclass. Metaclasses introduce an
48
+ extra level of complexity to code and in some cases (e.g. conflicts)
49
+ are a problem. So reducing the need for them is worth doing when the
50
+ opportunity presents itself. PEP 422 and PEP 487 discuss this at
51
+ length. Given that we now have a C implementation of ``OrderedDict``
52
+ and that ``OrderedDict`` is the common use case for ``__prepare__()``,
53
+ we have such an opportunity by defaulting to ``OrderedDict``.
54
+
55
+ Second, only classes that opt in to using the ``OrderedDict``-based
56
+ metaclass will have access to the definition order. This is problematic
57
+ for cases where universal access to the definition order is important.
49
58
One of the original motivating use cases for this PEP is generic class
50
59
decorators that make use of the definition order.
51
60
52
- Changing the default class definition namespace has been discussed a
53
- number of times, including on the mailing lists and in PEP 422 and
54
- PEP 487 (see the References section below).
55
-
56
61
57
62
Specification
58
63
=============
59
64
60
- * the default class *definition* namespace is now ``OrderdDict``
65
+ Part 1:
66
+
61
67
* the order in which class attributes are defined is preserved in the
62
68
new ``__definition_order__`` attribute on each class
63
69
* "dunder" attributes (e.g. ``__init__``, ``__module__``) are ignored
@@ -74,6 +80,10 @@ Specification
74
80
``OrderedDict`` (or a subclass) have their ``__definition_order__``
75
81
set to ``None`` (except where #1 applies)
76
82
83
+ Part 2:
84
+
85
+ * the default class *definition* namespace is now ``OrderdDict``
86
+
77
87
The following code demonstrates roughly equivalent semantics for the
78
88
default behavior::
79
89
0 commit comments